Building .NET common language runtime (CLR) routine code from the Db2 command window

You can build .NET CLR routine from the command line.

Before you begin

Attention: With the release of Db2 11.5.9, support for Microsoft .Net common language runtime (CLR) routines is deprecated and might be removed in the future. If you are currently running routines that have a dependency on .NET CLR, rewrite the routine logic in a supported language and then recreate the routines.

As an alternative to manually building the .NET CLR routines, you can use the sample build scripts that are provided in the sample subdirectory of the sqllib directory or build the routine in the Visual Studio .NET project.

The following conditions must be met before you can build the .NET CLR routines.
  • Prerequisite requirements for the operating system and the .NET CLR routine development software must be met.
  • You must have a .NET CLR routine source code that is written in a supported .NET CLR programming language.
  • You must have the database information in which the routines are to be created.
  • You must have compiler options that are operating system specific and link options that are required for building .NET CLR routines.

Procedure

To build a source code file that contains one or more .NET CLR routine code implementations:

  1. Open the Db2 command window.
  2. Navigate to the directory that contains your source code file.
  3. Establish a connection with the database in which the routines are created.
  4. Compile the source code file.
  5. Link the source code file to generate a shared library. The linking process requires the use of compiler link options that references the database include directory.
  6. Copy the assembly file with the .DLL file suffix to the database function directory on the database server.
  7. If you are not building the source code file for the first time, you must stop and restart the database manager to ensure that the new version of the shared library is used by the database manager. You can issue the db2stop command followed by the db2start command to restart the database manager.

Results

Once you successfully built and deployed the routine library, you can register the new routine in the database by running the CREATE statement. The routine must be registered before you can call the new routine.

Example

The following example builds a .NET CLR source code file. Steps are shown for both the myVBfile.vb Visual Basic code file and the myCSfile.cs C# code file that contains routine implementations. The routines are being built with the Microsoft .NET Framework on a Windows operating system to generate a 64-bit assembly.
  1. Open the Db2 command window.

  2. Navigate to the directory that contains your source code file.

  3. Establish a connection with the database to which the routine is intended for.
       Db2 connect to database-name
  4. Compile the source code file and specify any compiler options that you need to specify. You must reference the IBM® .NET data provider file location. In the following example. the $DB2PATH environment variable represents the IBM data server product installation path:
      C# example
      ===================
      csc /out:myCSfile.dll /target:library 
          /reference:$DB2PATH%\bin\netf40\IBM.Data.DB2.dll myCSfile.cs
    
      Visual Basic example
      ====================
      vbc /target:library /libpath:$DB2PATH\bin\netf40 
          /reference:$DB2PATH\bin\netf40\IBM.Data.DB2.dll 
          /reference:System.dll
          /reference:System.Data.dll myVBfile.vb
    

    The compiler displays an output to indicate whether there are any errors.

  5. Copy the shared library to the database function directory on the database server.
      C# example
      ====================
      rm -f ~HOME/sqllib/function/myCSfile.DLL		
      cp myCSfile $HOME/sqllib/function/myCSfile.DLL
    
      Visual Basic example
      ====================
      rm -f ~HOME/sqllib/function/myVBfile.DLL               
      cp myVBfile $HOME/sqllib/function/myVBfile.DLL          
    

    This step ensures that the routine library is in the default directory where the database manager looks for routine libraries.

  6. Stop and restart the database manager.
       db2stop
       db2start

You can modify the sample build scripts that are provided in the sample subdirectory of the sqllib directory to simplify building the .NET CLR routines.