• 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:TestLibsMSSQLExtMethod.RegexMSSQLExtMethod.RegexinDebugMSSQLExtMethod.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

  • 相关阅读:
    MongoDB下,启动服务时,出现“服务没有响应控制功能”解决方法
    如何安装mongodb.msi
    jade和ejs两者的特点
    Node.js中的http.request方法的使用说明
    34丨关于Linux网络,你必须知道这些(下)
    33 | 关于 Linux 网络,你必须知道这些(上)
    osi 七层模型 通俗易懂
    32 | 浅谈容器网络
    Linux 三剑客之SED行天下
    js 基本类型与引用类型的区别
  • 原文地址:https://www.cnblogs.com/niunan/p/8277896.html
Copyright © 2020-2023  润新知