利用SQLCLR创建表值函数读取img标签下的图片路径

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

public partial class ReadImgSrc
{
    public static String Img_Replace(string input)
    {
        string Pattern = @"<\s?img[^>]+?>";
        Regex reg = new Regex(Pattern, RegexOptions.IgnoreCase);
        MatchCollection colMatch = reg.Matches(input);
        string strImage = "";
        if (colMatch.Count > 0)
        {
            foreach (Match m in colMatch)
            {
                string html = Regex.Replace(m.Value, @"(?i)(<img[^>]*?src=(['""\s]?))((http://)?([^/\\]+[/\\])+?)([^\.]+\.[^'"">]+)\2([^>]*?>)", @"$3$6");
                strImage += html + ",";
            }
        }
        return strImage.TrimEnd(',');
    }

    [SqlFunction(TableDefinition = "path varchar(4000)", FillRowMethodName = "FillRow")]
    public static IEnumerable DoSplit(String htm)
    {
        return Img_Replace(htm).Split(',');
    }

    public static void FillRow(Object obj, out SqlChars path)
    {
        path = new SqlChars(obj.ToString());
    }

}


先打开VS2013, 敲好数据库项目altlib的代码,注意目标框架选择Framework 3.5


接着设置目标平台 Sql2008:

最后生成dll.嵌入Sqlserver 数据库的程序集中, 除填入dll的物理路径以外,就是选择程序集所有者为dbo.


再到Sql2008上安装sql 脚本.

EXEC sp_configure 'show advanced options', '1';
go
reconfigure;
go
EXEC sp_configure 'clr enabled', '1'
go
reconfigure;
EXEC sp_configure 'show advanced options', '1'; 
go
  ALTER DATABASE dbname set TRUSTWORTHY on;
go

EXEC sp_dbcmptlevel dbname, 90;

--创建表值函数  
CREATE FUNCTION dbo.ReadImage(@htm nvarchar(4000))  
RETURNS TABLE (path nvarchar(200))  
AS  
    EXTERNAL name altlib.ReadImgSrc.DoSplit
GO  



DECLARE @str VARCHAR(1000)
SET @str='<IMG height=375  src="http://www.mycreative.com.cn/upload/2010/4/tiancheng2.jpg" width=500><IMG  src="http://www.mycreative.com.cn/upload/2010/4/s2.jpg"> '
SET @str=@str+'<IMG  src="35/40/0130000025s.jpg" ></tr><tr><td></td></tr><tr><gggsgs><IMG  src="http://a0.att.hudong.com/35/40/0130000025s.jpg" width=88 >'
SELECT * FROM  dbo.ReadImage(@str) 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值