【发布时间】:2017-03-29 09:47:56
【问题描述】:
我将用户定义的表类型传递给存储过程。但它会引发错误,因为“不存在从对象类型 System.Collections.Generic.List`1 到已知托管提供程序本机类型的映射。”
UserDefinedTableType:
CREATE TYPE UserQueryAttachmentList AS TABLE
(
[UserQueryId] NVARCHAR(50),
[AttachmentPath] NVARCHAR(200),
[AttachmentName] NVARCHAR(200),
[AttachmentFor] NVARCHAR(50),
[CreatedBy] NVARCHAR(50)
);
Stored Procedure:
CREATE PROCEDURE PROC_UserQueryAttachment_Insert
(
@Table UserQueryAttachmentList READONLY
)
AS
BEGIN
INSERT INTO dbo.[UserQueryAttachments]
(
UserQueryId,
AttachmentPath,
AttachmentName,
AttachmentFor,
CreatedBy,
CreatedDate
)
SELECT
UserQueryId,
AttachmentPath,
AttachmentName,
AttachmentFor,
CreatedBy,
GETDATE()
FROM
@Table T
END
C#:
public override bool SaveUserQueryAttachment(List<UserQueryAttachmentToCreate> fileList)
{
try
{
this.ExecuteStoredProcedureOrQuery<UserQueryAttachmentToCreate>("PROC_UserQueryAttachment_Insert", CommandType.StoredProcedure,
new SqlParameter("@Table", fileList)
);
return true;
}
catch (Exception ex)
{
return false;
}
}
请指导我为什么会出现这个错误?
【问题讨论】:
-
您需要将 DataTable 发送到存储过程,而不是 List。
标签: c# sql-server asp.net-mvc stored-procedures user-defined-types