SQL Server数据库调用dll文件的解决办法
2022-11-12 09:38:54
内容摘要
这篇文章主要为大家详细介绍了SQL Server数据库调用dll文件的简单示例,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
1.首先新建一个空
文章正文
这篇文章主要为大家详细介绍了SQL Server数据库调用dll文件的简单示例,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
1.首先新建一个空的解决方案,并添加一个类库,代码如下,编译并生产dll
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <code> 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; } } } </code> |
2.启用CLR功能
默认情况下,SQL Server中的CLR是关闭的,所以我们需要执行如下命令打开CLR:
代码如下:
1 2 3 4 | <code> exec sp_configure 'clr enabled' ,1 reconfigure Go</code> |
3.将程序集引用到数据库中
代码如下:
1 2 | <code> CREATE ASSEMBLY testHelloWorld FROM 'C:\TEST.dll' --( 'C:/TEST.dll' w为错误写法)</code> |
4.创建函数
代码如下:
1 2 3 4 5 6 7 | <code> CREATE FUNCTION dbo.clrHelloWorld ( @name as nvarchar(200) ) RETURNS nvarchar(200) AS EXTERNAL NAME testHelloWorld.[TEST.TestTrans].GenerateDecryptString </code> |
5.调用函数
代码如下:
1 2 | <code> SELECT dbo.clrHelloWorld( '耿耿' ) </code> |
6.执行结果
HELLO WORLD 耿耿!
总结
以上所述是小编给大家介绍的Sql Server 数据库中调用dll文件的过程,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对512笔记网站的支持!
注:关于SQL Server数据库调用dll文件的简单示例的内容就先介绍到这里,更多相关文章的可以留意
代码注释