Building .NET common language runtime (CLR) routine code using sample build scripts

Building .NET common language runtime (CLR) routine source code is a subtask of creating .NET CLR routines. You can use the sample batch files that are provided with the database to build .NET CLR routines more easily.

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.

The sample build scripts can be used for source code with or without SQL statements. The build scripts take care of the compilation, linking, and deployment of the built assembly to the function directory.

As alternatives, you can build .NET CLR routines in Visual Studio .NET to simplify the task or you can issue the commands in the sample build script manually.

You can use the programming-language specific sample build script bldrtn for building C# and Visual Basic .NET CLR routines. They are located in the database sample directory along with sample programs:
  • For C: sqllib/samples/cs/
  • For C++: sqllib/samples/vb/
The bldrtn scripts can be used to build source code files containing both procedures and user-defined functions. The script does the following:
  • Establishes a connection with a user-specified database
  • Compiles and links the source code to generate an assembly with a .DLL file suffix
  • Copies the assembly file to the database function directory on the database server
The bldrtn scripts accept two arguments:
  • The name of a source code file without any file suffix
  • The name of a database to which a connection will be established

The database parameter is optional. If no database name is supplied, the program uses the default sample database. As routines must be built on the same instance where the database resides, no arguments are required for a user ID and password.

Prerequisites
  • The required .NET CLR routine operating system and development software prerequisites must be satisfied. See: "Support for .NET CLR routine development".
  • Source code file containing one or more routine implementations.
  • The name of the database within the current database instance in which the routines are to be created.
Procedure
To build a source code file that contains one or more routine code implementations, perform the following steps.
  1. Open the Db2 command window.
  2. Copy your source code file into the same directory as the bldrtn script file.
  3. If the routines will be created in the sample database, enter the build script name followed by the name of the source code file without the .cs or .vb file extension:
       bldrtn file-name
    If the routines will be created in another database, enter the build script name, the source code file name without any file extension, and the database name:
       bldrtn file-name database-name
    The script compiles and links the source code and produces an assembly. The script then copies the assembly to the function directory on the database server
  4. If this is not the first time that the source code file containing the routine implementations was built, stop and restart the database to ensure the new version of the shared library is used by the database. You can do this by entering db2stop followed by db2start on the command line.

Once you have successfully built the routine shared library and deployed it to the function directory on the database server, you should complete the steps associated with the task of creating C and C++ routines.

Creating .NET CLR routines includes a step for executing the CREATE statement for each routine that was implemented in the source code file. After routine creation is completed you can invoke your routines.