作用
从数据库发起对外部服务的请求.
应用场景
心跳监测
定时启动站点
服务实现
代码部分(C#)
using System;
using System.Net;
publicpartialclassStoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure]
publicstaticvoid UploadString(String url)
{
var client = newWebClient { Encoding = System.Text.Encoding.UTF8 };
client.UploadString(url, String.Empty);
}
}
注: Microsoft.SqlServer.Server.SqlProcedure属于System.Data程序集.
数据库部分
权限配置
USE [Exercise]
GO
ALTERDATABASE [Exercise] SETTRUSTWORTHYON
GO
EXECsp_configure'clr enabled',1
GO
sp_changedbowner'sa'
GO
reconfigure
GO
外部DLL引用配置
外部服务调用
1. 通过EXTERNAL关键字调用外部DLL的方法
CREATEPROCEDURE [dbo].[Exercise_SP_External_UploadString]
@Url [nvarchar](max)
WITHEXECUTEASCALLER
AS
EXTERNAL NAME [SqlserverClr.Service].[StoredProcedures].[UploadString]
GO
2. 以参数的形式调用外部服务
CREATEPROCEDURE [dbo].[Exercise_SP_CallExternalHttpService]
AS
BEGIN
EXEC dbo.[Exercise_SP_UploadString]@Url ='http://www.baidu.com/'
END
GO