我很难理解正确的Dapper语法,将包含TableValueParameter(TVP)的参数以及其他参数传递给存储过程。我的代码的简化版本:
类
public class GalleryViewModel
{
public string GalleryName { get; set; }
public string Category { get; set; }
public IEnumerable<Image> Images { get; set; }
}
public class Image
{
public string FileName { get; set; }
public int Order { get; set; }
}
C#Snippet
var param = new DynamicParameters();
param.Add("@Images", model.Images);//TVP
param.Add("@GalleryName", model.GalleryName);
param.Add("@Category", model.Category);
connection.Execute("CreateGallery", param, commandType: CommandType.StoredProcedure);
SQL代码段
--Create table type
CREATE TYPE GalleryImageType AS TABLE
(
FileName NVARCHAR(64),
Order INT
)
GO
CREATE PROCEDURE [dbo].[CreateGallery]
@Images GalleryImageType READONLY,
@GalleryName NVARCHAR(32),
@Category NVARCHAR(32)
AS
-- SPROC contents here....
答案 0 :(得分:3)
使用DataTable的实例而不是IEnumerable:
DataTable dt = new DataTable();
dt.Columns.Add("FileName");
dt.Columns.Add("Order");
foreach (var image in viewmodel.Images)
{
dt.Rows.Add(image.FileName, image.Order);
}
var param = new DynamicParameters();
param.Add("@Images", dt);//TVP
param.Add("@GalleryName", model.GalleryName);
param.Add("@Category", model.Category);
connection.Execute("CreateGallery", param, commandType: CommandType.StoredProcedure);