美文网首页
sql server中调用c#写的dll里的方法

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

作者: niunan | 来源:发表于2018-01-12 22:14 被阅读40次

    最近有一项目:

    一超市管理系统单机版,运行在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

    相关文章

      网友评论

          本文标题:sql server中调用c#写的dll里的方法

          本文链接:https://www.haomeiwen.com/subject/dfjqoxtx.html