怎么在Sql Server 数据库中调用dll文件?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

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

usingSystem;usingSystem.Collections.Generic;usingSystem.Data.SqlTypes;usingSystem.Linq;usingSystem.Text;namespaceTEST{publicclassTestTrans{[Microsoft.SqlServer.Server.SqlFunction]publicstaticSqlStringGenerateDecryptString(stringname){stringdecode=string.Empty;decode=string.Format("HELLOWORLD{0}!",name);//DecryptString(dataXML.Value);SqlStringsqlValue=newSqlString(decode);returnsqlValue;}}}

2.启用CLR功能

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

execsp_configure'clrenabled',1reconfigureGo

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

CREATEASSEMBLYtestHelloWorldFROM'C:\TEST.dll'--('C:/TEST.dll'w为错误写法)

4.创建函数

CREATEFUNCTIONdbo.clrHelloWorld(@nameasnvarchar(200))RETURNSnvarchar(200)ASEXTERNALNAMEtestHelloWorld.[TEST.TestTrans].GenerateDecryptString

5.调用函数

SELECTdbo.clrHelloWorld('耿耿')

6.执行结果

HELLO WORLD 耿耿!

看完上述内容,你们掌握怎么在Sql Server 数据库中调用dll文件的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!