using System; using System.Collections.Generic; using System.Text; using System.Data.SqlTypes; using System.Text.RegularExpressions; using Microsoft.SqlServer.Server; using System.Net; using System.IO; namespace qanholas { public class net { [SqlFunction(IsDeterministic = true , DataAccess = DataAccessKind.None)] public static SqlString ping( string html) { return (SqlString)GetWebClient(html); } private static string GetWebClient( string url) { string strHTML = "" ; WebClient myWebClient = new WebClient(); Stream myStream = myWebClient.OpenRead(url); StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding( " utf-8 " )); strHTML = sr.ReadToEnd(); myStream.Close(); return strHTML; } } }
--创建程序集
USE qanholas;
CREATE ASSEMBLY qanholas
FROM 'D:\WindowsApplication\qanholas\qanholas\bin\Debug\qanholas.dll'
--创建函数
CREATE FUNCTION dbo.gethtml
(
@html AS NVARCHAR( 20)
)
RETURNS NVARCHAR( MAX)
AS
EXTERNAL NAME qanholas.[qanholas.net].[ping];
GO
--无限制访问
USE [qanholas]
GO
ALTER ASSEMBLY [qanholas]
WITH PERMISSION_SET = UNSAFE
GO
SELECT dbo.gethtml('http:\\www.baidu.com')
