CLR创建函数

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Collections;
using Microsoft.SqlServer.Server;
using System.Text.RegularExpressions;

public partial class SqlFunction
{
    //匹配
    [Microsoft.SqlServer.Server.SqlFunction]
    public static SqlString IsMatch(string source,string pattern)
    {
        SqlString Rst;
        if (Regex.IsMatch(source, pattern) == true)
            Rst = "True";
        else
            Rst = "False";

        return Rst;
    }

    //替换
    [Microsoft.SqlServer.Server.SqlFunction]
    public static SqlString MyReplace(string source, string pattern, string value)
    {
        return Regex.Replace(source, pattern, value);
    }

    //分割
    [Microsoft.SqlServer.Server.SqlFunction(FillRowMethodName = "FillRow",TableDefinition="ZFCC_C nvarchar(100)")]
    public static IEnumerable Splict(string source, string pattern)
    {
        return Regex.Split(source, pattern);
    }
    public static void FillRow(object obj, out SqlString str)
    {
        str = new SqlString ((string )obj); 
    }
    //统计
    [Microsoft.SqlServer.Server.SqlFunction]
    public static SqlInt32 Occurs(string source, string pattern)
    {
        return Regex.Matches(source, pattern).Count;  
    }

    //截取
    [Microsoft.SqlServer.Server.SqlFunction]
    public static SqlString MySubstr(string source, string pattern)
    {
        string Rst="";
        MatchCollection MCollection;
        MCollection = Regex.Matches(source, pattern);
        foreach (Match  M in MCollection)
            Rst+=M.Value;
        return Rst;
    }

    //排序
    [Microsoft.SqlServer.Server.SqlFunction]
    public static SqlString MySort(string source)
    {
        string Rst="";
        string[] Arr = new string[source.Length];
        for (int i = 0; i < source.Length; i++)
            Arr[i] = source.Substring(i, 1);
        Array.Sort(Arr);
        foreach (string S in Arr)
            Rst += S;

        return Rst;
    }
};

 

--Sql Server 2005

Create Assembly CLRFunction From 'F:\Mxbing1984\C#\CLR\CLRFunction.Dll' 
With Permission_Set=External_Access
GO
Create Function IsMatch(@Source nvarchar(100),@Pattern nVarchar(10))
Returns
As
External Name CLRFunction.CLRFunction.IsMatch
GO

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值