简体   繁体   English

SQL Server中的CLR汇编C#

[英]CLR Assembly C# inside SQL Server

  • Is it possible to make a big project in C# (lots of functions), then, 是否可以使用C#(大量函数)创建一个大型项目,然后,
  • Create CLR Assembly for it, then, 为此为其创建CLR程序集,
  • In SQL Server IN A STORED PROC, call a function that is in the assembly, 在“存储过程中的SQL Server”中,调用程序集中的函数,
  • The table (which I would pass to ASSEMBLY) is computed in other stored proc... 该表(我将传递给ASSEMBLY的表)是在其他存储过程中计算的...

    • If so, What would be the steps? 如果是这样,将采取什么步骤?

I am thinking something like this. 我在想这样的事情。

-- I have a stored proc that gets a table, let it be myStoredProcTable

--FIST ENABLE DATA ACCESS
EXEC sp_serveroption 'TheServerName', 'DATA ACCESS', TRUE

--main.sql calls yStoredProcTable.sql and the calls functionAssembly
SELECT  *
INTO    #tmpTable
FROM    OPENQUERY(SERVERNAME, 'EXEC test.dbo.myStoredProcTable 1')


-- pass the table to assembly
-- how would i pass the table to assembly code?, Is this POSSIBLE?
    EXEC functionAssembly #tmpTable

------------------------------------------edit - - - - - - - - - - - - - - - - - - - - - 编辑

Following @faester answer: - How could I use XML in the code, I suggested to use the numberTOstring thing, but I guess XML option is the best here... @faester回答如下:-如何在代码中使用XML,我建议使用numberTOstring ,但我想XML选项是最好的选择...

Again, I really do this, even if is not the best choice... 再说一次,即使不是最佳选择,我也确实做到了。

Yes you can register assemblies, but it is rarely a good idea due to performance issues. 是的,您可以注册程序集,但是由于性能问题,这很少是一个好主意。

But if you make complex numeric calculations or similar operations on scalar values it can give you a lot of flexibility. 但是,如果您对标量值进行复杂的数字计算或类似的操作,则可以为您带来很大的灵活性。 But the problem remains that SQL is natively set oriented which C# isn't, so you will easily run into mismatches. 但是问题仍然在于,SQL本质上是面向集合的,而C#不是,因此您很容易遇到不匹配的情况。

You should also be aware that you can only import static members on static classes. 您还应该知道,您只能在静态类上导入静态成员。

But an example This class - which intentionally doesn't have a namespace since it seems to be impossible to import classes in a namespace. 但是有一个例子此类-故意没有名称空间,因为似乎无法在名称空间中导入类。

    public static class Math
    {
        [Microsoft.SqlServer.Server.SqlFunction]
        public static int Add(int a, int b)
        {
            return a + b;
        }


        [Microsoft.SqlServer.Server.SqlProcedure]
        public static void Void(int a, int b)
        {
        }
    }

It takes some SQL to get the server ready and you probably need to be admin. 需要一些SQL才能准备好服务器,您可能需要成为管理员。

    EXEC SP_CONFIGURE 'clr enabled', 1
    GO
    RECONFIGURE
    GO
    -- CONSIDER: DROP ASSEMBLY SqlClr
    GO
    CREATE ASSEMBLY SqlClr 
    FROM 'pathtoassembly'
    WITH PERMISSION_SET = SAFE;
    GO
    SELECT * FROM sys.assemblies
    GO
    CREATE FUNCTION [MathAdd] (@a int, @b int)
    RETURNS INT 
    AS EXTERNAL NAME [SqlClr].Math.[Add]
    GO 
    CREATE PROCEDURE [Void] @a INT, @b INT
    AS EXTERNAL NAME [SqlClr].Math.[Void]
    GO 
    SELECT dbo.MathAdd (1, 2)
    EXEC void 1, 2

AGAIN: You really should be confident that you need this, it is rarely a good idea! 再次:您真的应该确信自己需要这个,这绝不是一个好主意! (I have used it once for email validation making dns lookups etc, but that was on a system where all business logics was written in SQL. And that is bad!) (我曾经将它用于电子邮件验证以及dns查找等工作,但这是在所有业务逻辑都用SQL编写的系统上的。这很糟糕!)

Some useful references: 一些有用的参考:

http://msdn.microsoft.com/en-us/library/ms189524.aspx http://msdn.microsoft.com/en-us/library/ms189524.aspx

http://www.codeproject.com/KB/database/CLR_in_Sql_Server_2005.aspx http://www.codeproject.com/KB/database/CLR_in_Sql_Server_2005.aspx

使用SQL CLR执行您描述的确切功能。

It is possible . 有可能 (But read the comments). (但请阅读评论)。

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

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