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  耿耿!

总结

以上所述是www.887551.com给大家介绍的sql server 数据库中调用dll文件的过程,希望对大家有所帮助

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐