Loading and calling C # (assembly) from sql stored procedure

Suppose I have a simple C # code to print HELLO WORLD as shown here

using System; using System.Data; using Microsoft.SqlServer.Server; using System.Data.SqlTypes; public class HelloWorldProc { [Microsoft.SqlServer.Server.SqlProcedure] public static void HelloWorld() { SqlContext.Pipe.Send("HELLO WORLD!\n"); } } 

Therefore they say on this page:

This section provides an overview of the namespace and libraries required to compile database objects using Microsoft SQL Server Integration with the Microsoft.NET Framework (CLR). The topic also shows how to write, compile and follow a simple CLR storage procedure written in Microsoft Visual C #.

CLR integration functionality is exposed in an assembly called system.data.dll, which is part of the .NET Framework. This assembly can be found in the global assembly cache (GAC), as well as in the .NET Framework directory. A reference to this assembly is usually added automatically by both tool commands and Microsoft Visual Studio, so there is no need to add it manually.

After creating (compiling and adding to the dll path), you can use this code as a stored procedure, for example:

 CREATE ASSEMBLY helloworld from 'c:\helloworld.dll' WITH PERMISSION_SET = SAFE 

Once the assembly has been created, we can now access our HelloWorld method using the create procedure statement. We will call our stored procedure "hello":

 CREATE PROCEDURE hello AS EXTERNAL NAME helloworld.HelloWorldProc.HelloWorld 

Once a procedure has been created, it can be started as a regular stored procedure written in Transact-SQL. Run the following command:

 EXEC hello 

To remove this proc:

 drop procedure hello 

Once the procedure has been deleted, you can delete the assembly containing your sample code.

 drop assembly helloworld 

After this introduction:

I need to use C ++ code that also uses extern dll, and my question is this:

How could I use my C ++ code (which also uses extern dll) inside this C #, as in the example, so after creating the assembly I am currently executing the saved process, the following is transparent:

  • SQL creates the assembly and then calls the C # code inside the dll.
  • C # code calls C ++ code (inside dll)
  • C ++ code calls extern dll.

Therefore i only do

EXEC hello

and everything that happens (SQL SERVER -> C # -> C ++ -> extern dll).

  • How to make this whole dll page continue and coexist, so I only execute the stored procedure on SQL Server, for example EXEC hi

I thought in

 [DllImport("cCode.dll")] 

but if C ++ code depends on exll dll ... I get lost

+4
source share
1 answer

Well, if you are going to follow this path, you can skip using C # as a wrapper for your C ++ dll (since it looks like you are just trying to use SQL Server CLR integration as a means to call into a C ++ routine).

Instead, you can create and register a C ++ COM + application ( example ) on the server and call it from SQL Server using sp_OACreate and its associated procedures . FYI: you can also create a COM + application in C # if you want, but it can be more work with the interface with C ++ dll.

I think you would be better off with this approach, because the COM + application can be configured to run in a dedicated server, not in a SQL Server process.

Perhaps your situation dictates differently, but, as other people comment, it is better to leave this "integration" outside the database and put it in a separate application level.

+4
source

All Articles