简体   繁体   English

从 sql 存储过程加载和调用 C#(程序集)

[英]loading and calling C# (assembly) from a sql stored proc

suppose I have simple C# code to print HELLO WORLD as shown in here假设我有简单的C# 代码来打印 HELLO WORLD,如下所示

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");
    }
}

So they say in that page:所以他们在那一页上说:

This topic provides an overview of the namespaces and libraries required to compile database objects using the Microsoft SQL Server integration with the Microsoft .NET Framework common language runtime (CLR).本主题概述了使用 Microsoft SQL 服务器与 Microsoft .NET 框架公共语言运行时 (CLR) 集成来编译数据库对象所需的命名空间和库。 The topic also shows you how to write, compile, and run a simple CLR stored procedure written in Microsoft Visual C#.本主题还向您展示了如何编写、编译和运行用 Microsoft Visual C# 编写的简单 CLR 存储过程。

CLR integration functionality is exposed in an assembly called system.data.dll, which is part of the .NET Framework. CLR 集成功能在名为 system.data.dll 的程序集中公开,它是 .NET 框架的一部分。 This assembly can be found in the Global Assembly Cache (GAC) as well as in the .NET Framework directory.此程序集可以在全局程序集缓存 (GAC) 以及 .NET 框架目录中找到。 A reference to this assembly is typically added automatically by both command line tools and Microsoft Visual Studio, so there is no need to add it manually.对此程序集的引用通常由命令行工具和 Microsoft Visual Studio 自动添加,因此无需手动添加。

After create, (compiling it and add to path dll's) you can use that code as a Stored Procedure like:创建后,(编译它并添加到路径 dll 中)您可以将该代码用作存储过程,例如:

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

Once the assembly has been created, we can now access our HelloWorld method by using the create procedure statement.创建程序集后,我们现在可以使用 create procedure 语句访问我们的 HelloWorld 方法。 We will call our stored procedure "hello":我们将调用我们的存储过程“hello”:

CREATE PROCEDURE hello
AS
EXTERNAL NAME helloworld.HelloWorldProc.HelloWorld

Once the procedure has been created, it can be run just like a normal stored procedure written in Transact-SQL.一旦创建了过程,就可以像使用 Transact-SQL 编写的普通存储过程一样运行它。 Execute the following command:执行以下命令:

EXEC hello

To drop that proc:要删除该过程:

drop procedure hello

Once the procedure has been dropped, you can remove the assembly containing your sample code.删除该过程后,您可以删除包含示例代码的程序集。

drop assembly helloworld

After this introduction:介绍完之后:

I need to use a C++ code that also uses an extern dll, and my question is the following,我需要使用还使用外部 dll 的 C++ 代码,我的问题如下,

How could I use my C++ code (that also uses extern dll) inside that C# like in the example, so, after creating assembly at the moment I exec the stored proc, the following is transparant:我怎么能像示例中一样在 C# 中使用我的 C++ 代码(也使用 extern dll),所以,在创建程序集之后,我执行存储的过程,以下是透明的:

  1. SQL creates the assembly and then calls the C# code inside a dll. SQL 创建程序集,然后在 dll 中调用 C# 代码。
  2. C# code calls C++ code (inside a dll) C# 代码调用 C++ 代码(在 dll 内)
  3. The C++ code calls extern dll. C++ 代码调用 extern dll。

So I only do所以我只做

EXEC hello执行你好

and all that happens (SQL SERVER -> C# -> C++ -> extern dll).以及所有发生的事情(SQL SERVER -> C# -> C++ -> extern dll)。

  • How to make all this dll party go on and coexist, so I only execute a Stored proc in SQL Server like EXEC hello如何使所有这些 dll 方 go 开启并共存,所以我只在 SQL 服务器中执行存储过程,如EXEC hello

I was thinking in我在想

[DllImport("cCode.dll")]

but if the c++ code depends on an extern dll... I am getting lost但是如果 c++ 代码取决于外部 dll... 我迷路了

Well, if you're going to go down this path, you might skip over using C# as a wrapper for your C++ dll (since it sounds like you're just trying to use the SQL Server CLR integration as a means to call into a C++ routine). Well, if you're going to go down this path, you might skip over using C# as a wrapper for your C++ dll (since it sounds like you're just trying to use the SQL Server CLR integration as a means to call into a C++ 例程)。

Instead, you could create and register a C++ COM+ Application ( example ) on the server and invoke this from SQL Server using sp_OACreate and related procedures .相反,您可以在服务器上创建和注册 C++ COM+ 应用程序( 示例),并使用sp_OACreate和相关过程从 SQL 服务器调用它。 FYI: You could also create the COM+ Application in C# if you wanted, but it might be more work to interface with the C++ dll.仅供参考:如果需要,您也可以在 C# 中创建 COM+ 应用程序,但与 C++ dll 接口可能需要更多工作。

I think you will have better luck with this approach because a COM+ Application can be configured to run in a dedicated server process instead of within the SQL Server process.我认为使用这种方法会更好,因为 COM+ 应用程序可以配置为在专用服务器进程中运行,而不是在 SQL 服务器进程中运行。

Maybe your situation dictates otherwise, but as other people have commented, it's best to leave this kind of "integration" outside of the database and put it into a separate application layer.也许您的情况另有规定,但正如其他人所评论的那样,最好将这种“集成”留在数据库之外并将其放入单独的应用程序层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM