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

最近有一项目:

 一超市管理系统单机版,运行在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](@input,@regex,@replace)
select dbo.[Regex.Math](@input2,@regex2)
select dbo.[Regex.IsMath](@input2,@regex2)




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

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


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


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


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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用SQL Server的CLR集成功能来实现发送HTTP请求。CLR集成是SQL Server提供的一种机制,允许开发人员使用.NET编程语言编存储过程、函数和触发器等托管代码。 以下是一个简单的示例,演示如何使用CLR集成发送HTTP请求: 1. 创建一个C#类库项目,添加以下代码: ``` using System; using System.Data.SqlTypes; using System.Net; public class HttpHelper { [Microsoft.SqlServer.Server.SqlFunction] public static SqlString SendHttpRequest(SqlString url) { try { WebClient client = new WebClient(); string response = client.DownloadString(url.ToString()); return new SqlString(response); } catch (Exception ex) { return new SqlString(ex.Message); } } } ``` 2. 在项目添加引用 System.Data和 System.Data.SqlServer。 3. 在项目属性,选择“生成”选项卡,将目标框架设置为.NET Framework 4.0或更高版本。选择“生成”菜单,生成项目。 4. 将DLL文件添加到SQL Server。在SQL Server Management Studio,右键单击“数据库”节点,选择“新建查询”。 5. 输入以下命令,创建一个程序集: ``` CREATE ASSEMBLY HttpHelper FROM 'C:\Path\To\HttpHelper.dll' WITH PERMISSION_SET = SAFE; ``` 6. 创建一个存储过程,调用CLR函数: ``` CREATE PROCEDURE GetHttpData (@url NVARCHAR(200)) AS EXTERNAL NAME HttpHelper.[HttpHelper].SendHttpRequest; ``` 7. 调用存储过程: ``` EXEC GetHttpData 'http://www.example.com'; ``` 执行以上命令,即可通过SQL Server调用CLR函数发送HTTP请求,并返回响应结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值