我创建了一个返回一组记录的 .Net 程序集。当我在 SQL Server 中创建函数时,我收到消息
CREATE FUNCTION for ...失败,因为返回值的 T-SQL 和 CLR 类型不匹配。
我有点迷茫,有人知道出了什么问题吗?这是我的程序集:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Server;
using System.Net;
using System.IO;
using System.Data.SqlTypes;
using System.Net.Json;
namespace SQLHttpRequest
{
public partial class HTTPFunctions
{
public class mailBox
{
public bool Expanded { get; set; }
public string ID { get; set; }
public string Name { get; set; }
public string ParentID { get; set; }
public int ChildFolderCount { get; set; }
public int UnreadItemCount { get; set; }
public int TotalItemCount { get; set; }
}
public static void FillRow(object mailBoxObject
, out SqlBoolean Expanded
, out SqlString ID
, out SqlString Name
, out SqlString ParentID
, out SqlInt32 ChildFolderCount
, out SqlInt32 UnreadItemCount
, out SqlInt32 TotalItemCount
)
{
var mb = (mailBox)mailBoxObject;
Expanded = mb.Expanded;
ID = mb.ID;
Name = mb.Name;
ParentID = mb.ParentID;
ChildFolderCount = mb.ChildFolderCount;
UnreadItemCount = mb.UnreadItemCount;
TotalItemCount = mb.TotalItemCount;
}
[Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read
, TableDefinition = "Expanded BIT, ID NVARCHAR(100),Name NVARCHAR(max),ParentID NVARCHAR(max),ChildFolderCount INT, UnreadItemCount INT, TotalItemCount INT"
, FillRowMethodName = "FillRow")]
public static List<mailBox> GetMailFolders(string WellKnownFolderName, string FolderID, string UserID, string password)
{
List<mailBox> mbs = ReadFolder(WellKnownFolderName, UserID, password);
// Method ReadFolder Fills the mbs List
return mbs;
}
}
}
程序集接收一些参数并返回一个邮件文件夹列表。在 SQL Server 中成功创建程序集:
CREATE ASSEMBLY SQLHttpRequest
FROM N'C:\...\SQLHttpRequest.dll'
WITH PERMISSION_SET=UNSAFE;
当我尝试创建函数时
CREATE FUNCTION dbo.usp_GetMailFolders(
@WellKnownFolderName nvarchar(max),
@FolderID nvarchar(max),
@UserID nvarchar(max),
@password nvarchar(max)
)
RETURNS TABLE
(
Expanded BIT,
ID NVARCHAR(100),
Name NVARCHAR(100),
ParentID NVARCHAR(100),
ChildFolderCount int,
UnreadItemCount int,
TotalItemCount int
)
AS
EXTERNAL NAME SQLHttpRequest.[SQLHttpRequest.HTTPFunctions].GetMailFolders;
我收到以下错误:
“usp_GetMailFolders”的 CREATE FUNCTION 失败,因为返回值的 T-SQL 和 CLR 类型不匹配。
谁能发现我在这里遗漏了什么?
预先感谢您的回复!
最佳答案
尝试将函数 GetMailFolders 的签名更改为 IEnumerable。
public static IEnumerable GetMailFolders(string WellKnownFolderName, string FolderID, string UserID, string password)
我又看了一遍,在我看来,尝试在代码中使用 nvarchar 而不是 varchar 来创建工具。
关于c# - CREATE FUNCTION 失败,因为返回值的 T-SQL 和 CLR 类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39765225/