sqlserver 2016 http请求
时间: 2023-09-28 10:09:30 浏览: 51
SQL Server 2016 支持使用 HTTP 请求从外部系统或 Web 服务中获取数据。这可以通过以下步骤实现:
1.启用 SQL Server 数据库引擎中的外部数据源:
```
sp_configure 'clr enabled', 1;
GO
RECONFIGURE;
GO
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
```
2.创建一个存储过程,该存储过程将使用 CLR(公共语言运行时)进行编写,以获取 HTTP 请求的响应。下面是一个简单的示例存储过程:
```
CREATE PROCEDURE dbo.GetHttpResponse
@url NVARCHAR(2000)
AS
EXTERNAL NAME HttpRequests.HttpRequest.GetHttpResponse;
```
3.创建 CLR 程序集,以便在 SQL Server 中使用。以下是一个简单的 C# 程序集示例:
```
using System.Net;
using System.IO;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;
public class HttpRequest
{
[SqlProcedure]
public static void GetHttpResponse(SqlString url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.Value);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
SqlContext.Pipe.Send(reader.ReadToEnd());
}
}
```
4.将 CLR 程序集部署到 SQL Server 中:
```
CREATE ASSEMBLY HttpRequests FROM 'C:\HttpRequests.dll' WITH PERMISSION_SET = UNSAFE;
```
5.测试存储过程:
```
EXEC dbo.GetHttpResponse 'http://www.example.com';
```
这将从 http://www.example.com 获取响应,并将其发送回 SQL Server。