【发布时间】:2018-10-14 08:21:10
【问题描述】:
第一次发帖,请多指教! :-) VB/C# 的新手,尝试修复调用 Web 服务的 CLR 函数。我拼凑了以下内容:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Collections;
using System.Globalization;
// For the SQL Server integration
using Microsoft.SqlServer.Server;
// Other things we need for WebRequest
using System.Net;
using System.Text;
using System.IO;
public partial class UserDefinedFunctions
{
// Function to return a web URL as a string value.
[Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)]
public static SqlChars GET(SqlString uri, SqlString username, SqlString passwd)
{
// The SqlPipe is how we send data back to the caller
SqlPipe pipe = SqlContext.Pipe;
SqlChars document;
// Set up the request, including authentication
WebRequest req = WebRequest.Create(Convert.ToString(uri));
if (Convert.ToString(username) != null & Convert.ToString(username) != "")
{
req.Credentials = new NetworkCredential(
Convert.ToString(username),
Convert.ToString(passwd));
}
((HttpWebRequest)req).UserAgent = "CLR web client on SQL Server";
// Fire off the request and retrieve the response.
// We'll put the response in the string variable "document".
WebResponse resp = req.GetResponse();
Stream dataStream = resp.GetResponseStream();
StreamReader rdr = new StreamReader(dataStream);
document = new SqlChars(rdr.ReadToEnd());
if (document.IsNull)
{
return SqlChars.Null;
}
// Close up everything...
rdr.Close();
dataStream.Close();
resp.Close();
// .. and return the output to the caller.
return (document);
}
即使它是为 sqlChars 编码的,它也只会返回 4000 个字符。在完整的库存拉取期间,API 调用可能会返回 14MB 以上。如果我理解正确 sqlChars 转到 varchar(max) (或 nvarchar(max)) ... 我在这里想念什么?如果有帮助的话,在 sql server 2005 上为 .Net 3.0 编译... 安迪
【问题讨论】:
-
查看
resp.StatusCode= 206而不是200。 -
试试
new SqlChars(rdr.ReadToEnd().ToCharArray())。如果可行,我会为它做一个答案。 -
状态为 200 OK。 ToCharArray 返回相同的 4000 个字符。
标签: c# sql-server web-services sqlclr