Sql Server 数据库中怎么调用dll文件
更新:HHH   时间:2023-1-7


Sql Server 数据库中怎么调用dll文件,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

1.首先新建一个空的解决方案,并添加一个类库,代码如下,编译并生产dll

using System; using System.Collections.Generic; using System.Data.SqlTypes; using System.Linq; using System.Text; namespace TEST {   public class TestTrans   {     [Microsoft.SqlServer.Server.SqlFunction]     public static SqlString GenerateDecryptString(string name)     {       string decode = string.Empty;       decode = string.Format("HELLO WORLD {0}!", name);//DecryptString(dataXML.Value);       SqlString sqlValue = new SqlString(decode);       return sqlValue;     }   } }

2.启用CLR功能

默认情况下,SQL Server中的CLR是关闭的,所以我们需要执行如下命令打开CLR:

 exec sp_configure 'clr enabled',1   reconfigure   Go

3.将程序集引用到数据库中

CREATE ASSEMBLY testHelloWorld FROM 'C:\TEST.dll'   --('C:/TEST.dll'w为错误写法)

4.创建函数

CREATE FUNCTION dbo.clrHelloWorld   (     @name as nvarchar(200)   )   RETURNS nvarchar(200)   AS EXTERNAL NAME testHelloWorld.[TEST.TestTrans].GenerateDecryptString

5.调用函数

SELECT dbo.clrHelloWorld('耿耿')

6.执行结果

HELLO WORLD 耿耿!

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注天达云行业资讯频道,感谢您对天达云的支持。

返回数据库教程...