为了账号安全,请及时绑定邮箱和手机立即绑定

sql server中调用c#写的dll里的方法

标签:
SQL Server C#

最近有一项目:

一超市管理系统单机版,运行在WIN2003+SQL2005上,每天超市关门都都会关电脑,现客户要新加功能,每天关门下班后回家可以上网查看超市管理系统的数据库里的相关数据,然后再做一些原系统没有的统计分析等,老系统不能做大改动,像升级到WIN2012+SQL2012等这些操作,改动越小越好。

现在的想法是:阿里云买台服务器,装上SQL,然后建立的数据库和超市管理系统上的数据库一毛一样,然后想办法,当超市管理系统数据库里的增表增删改的时候,同步阿里云服务器上的数据库保持数据一致,

然后剩下的就是做自己的网站连接阿里云上的数据库做统计分析就好了

上网到处问网友,以前的技术经理给了如下方案:

using System.Text.RegularExpressions;

namespace MSSQLExtMethod
{
public class RegexExtends
{
[Microsoft.SqlServer.Server.SqlFunction]
public static bool IsMath(string input, string patten)
{
return !string.IsNullOrEmpty(input) && new Regex(patten).IsMatch(input);
}
[Microsoft.SqlServer.Server.SqlFunction]
public static string Math(string input, string patten)
{
return string.IsNullOrEmpty(input) ? "" : new Regex(patten).Match(input).Value;
}
[Microsoft.SqlServer.Server.SqlFunction]
public static string Replace(string input, string patten, string replace)
{
return string.IsNullOrEmpty(input) ? "" : new Regex(patten).Replace(input, replace);
}
}
}


create assembly Regex from 'E:\Test\Libs\MSSQLExtMethod.Regex\MSSQLExtMethod.Regex\bin\Debug\MSSQLExtMethod.Regex.dll' with permission_set = SAFE

exec sp_configure 'clr enabled',1
RECONFIGURE

create function [dbo].[Regex.Math](@Input nvarchar(max),@Regex nvarchar(max))
returns nvarchar(max) with execute as caller
as
external NAME [Regex].[MSSQLExtMethod.RegexExtends].[Math]
go

create function [dbo].[Regex.Replace](@Input nvarchar(max),@Regex nvarchar(max),@Replace nvarchar(max))
returns nvarchar(max) with execute as caller
as
external NAME [Regex].[MSSQLExtMethod.RegexExtends].[Replace]
go

create function [dbo].[Regex.IsMath](@Input nvarchar(max),@Regex nvarchar(max))
returns bit with execute as caller
as
external NAME [Regex].[MSSQLExtMethod.RegexExtends].[IsMath]
go


declare
@regex nvarchar(500)
,@replace nvarchar(500)
,@input nvarchar(500)
,@regex2 nvarchar(500)
,@input2 nvarchar(500)

select @regex = '^(1[3456789][0-9])[0-9]{4}([0-9]{4})$'
,@input = '13912345678'
,@replace = '$1****$2'

select @regex2='1[3456789][0-9]{6}'
,@input2='13100000000,13922222222'

select dbo.Regex.Replace
select dbo.Regex.Math
select dbo.Regex.IsMath

第一块代码,.net 类库,第二块代码类库注册到 MSSQL 中形成函数,第三块代码调用实例

今天自己测试了一下,发现只有.NET 2.0的DLL才可以,开始我是.NET 4.0的,总是报那个什么什么权限错误之类的。。

这样只要在SQL2005的表中加个触发器,有数据变动的时候就调用DLL里的方法访问远程接口进行增删改远程数据库就好了

另SQL非免费版里好像有个‘镜像’功能,和一个‘复制’功能,不知道能不能用,没有学过的。。。

自己在VS2W017中做的.NET 2.0的DLL示例方法的源码:

http://ohpxbzczu.bkt.clouddn.com/SQL2005ExecDLLDemo.zip

点击查看更多内容
3人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
全栈工程师
手记
粉丝
20
获赞与收藏
97

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消