SQL Server中调用C#类中的方法实例(使用.NET程序集)
作者:junjie 时间:2024-01-23 17:24:56
需求是这样的,我在.net程序里操作数据时将一些字段数据加密了,这些数据是很多系统共用的,其中一delphi程序也需要用到,并且需要将数据解密,由于我在.net里加密的方式比较特殊,在delphi程序里解密比较繁琐且要消耗很多时间,所以不得不让sqlserver调用程序集的方式来解决问题。
下面只是一个例子,贴出来共享。
建立一个dll,class,代码如下:
namespace MyDll
{
public partial class MyClass
{
[SqlMethod]
public static SqlString UrlDecode(string value)
{
return new SqlString(HttpUtility.UrlDecode(value));
}
}
}
放到数据库服务器上。
之后运行下面的T-SQL代码:
EXEC sp_configure 'show advanced options','1';
GO
RECONFIGURE;
GO
EXEC sp_configure 'clr enabled','1'
RECONFIGURE;
GO
ALTER DATABASE DBName SET TRUSTWORTHY ON
GO
CREATE ASSEMBLY
[System.Web] FROM
'C:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727\System.Web.dll'
WITH permission_set = UNSAFE
GO
CREATE ASSEMBLY SQL_CLR_Url
FROM 'C:\MyDLL.dll'
WITH PERMISSION_SET = UNSAFE
GO
CREATE FUNCTION SqlUrlDecode(@urlstr NVARCHAR(4000))
RETURNS NVARCHAR(4000)
AS
EXTERNAL NAME SQL_CLR_Url.[SqlClr.MyClass].UrlDecode
GO
然后就可以在T-SQL里调用这个函数了。
select dbo.SqlUrlDecode(Name) from Table
标签:SQL,Server,.NET程序集
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在go文件服务器加入http.StripPrefix的用途介绍
2023-06-26 07:19:07
![](https://img.aspxhome.com/file/2023/1/92031_0s.jpg)
PyCharm配置mongo插件的方法
2023-08-16 19:37:05
![](https://img.aspxhome.com/file/2023/6/85026_0s.jpg)
ASP实现SQL语句日期格式的加减运算
2008-11-07 15:13:00
Python基础中所出现的异常报错总结
2023-07-14 05:33:47
![](https://img.aspxhome.com/file/2023/0/107490_0s.png)
thinkphp框架实现删除和批量删除
2024-06-07 15:29:22
![](https://img.aspxhome.com/file/2023/4/122484_0s.jpg)
python获取各操作系统硬件信息的方法
2021-03-29 17:09:05
在Django model中设置多个字段联合唯一约束的实例
2021-02-09 22:04:59
Python figure参数及subplot子图绘制代码
2023-09-14 17:13:00
![](https://img.aspxhome.com/file/2023/4/67814_0s.png)
MySQL学习之索引及优化
2024-01-26 21:42:30
![](https://img.aspxhome.com/file/2023/9/106589_0s.png)
matplotlib绘图实例演示标记路径
2021-10-18 08:51:04
![](https://img.aspxhome.com/file/2023/8/76868_0s.png)
python实现3D地图可视化
2023-08-27 10:24:50
![](https://img.aspxhome.com/file/2023/6/81806_0s.jpg)
Pyhton中单行和多行注释的使用方法及规范
2021-11-21 12:13:00
![](https://img.aspxhome.com/file/2023/0/135310_0s.jpg)
python selenium 对浏览器标签页进行关闭和切换的方法
2023-06-18 19:42:26
初步认识Python中的列表与位运算符
2022-06-28 18:10:51
![](https://img.aspxhome.com/file/2023/4/90854_0s.jpg)
sql语句将数据库一条数据通过分隔符切割成多列方法实例
2024-01-15 02:24:45
![](https://img.aspxhome.com/file/2023/7/116287_0s.png)
win10 安装mysql 8.0.18-winx64的步骤详解
2024-01-24 06:54:04
![](https://img.aspxhome.com/file/2023/4/70384_0s.jpg)
最新解决'nvidia-smi' 不是内部或外部命令也不是可运行的程序
2022-07-01 16:30:06
![](https://img.aspxhome.com/file/2023/9/125159_0s.png)
简单的命令查看安装的python版本号
2022-03-12 17:23:06
![](https://img.aspxhome.com/file/2023/8/88838_0s.png)
微信跳一跳php代码实现
2024-06-05 09:46:20
Python画柱状统计图操作示例【基于matplotlib库】
2021-04-06 21:53:50
![](https://img.aspxhome.com/file/2023/2/97262_0s.png)