我有一个过程,其中包含一个表值参数,以及其他参数:
@H_502_2@CREATE PROCEDURE [dbo].[Update_Records]
@currentYear INT,@country INT,@records Record_Table_Type READONLY
AS
我试图用Dapper.TVP来称呼它.
以下是我到目前为止的代码:
@H_502_2@var recordsParameter = new List<sqlDataRecord>(); // This Metadata matches 'Record_Table_Type' in the Database var recordsMetaData = new[] { new sqlMetaData("OriginalValue",sqlDbType.Decimal,19,4),new sqlMetaData("NewValue",new sqlMetaData("NewPercent",7,2),}; foreach (var r in records) { var record = new sqlDataRecord(recordsMetaData); record.SetDecimal(0,r.OriginalValue); record.SetDecimal(1,r.NewValue); record.SetDecimal(2,r.NewPercent); recordsParameter.Add(record); } var spParams = new DynamicParameters(new { currentYear = filter.currentYear,country = filter.country,}); var recordsParam = new TableValueParameter("@records","Record_Table_Type",recordsParameter); using (var connection = ConnectionFactory.GetConnection()) { connection.Execute("Update_Records",???,commandType: CommandType.StoredProcedure); }我的问题是如何将两组参数传递给调用Dapper Execute()的过程?
我努力了:
@H_502_2@var spParams = new DynamicParameters(new { currentYear = filter.currentYear,records = new TableValueParameter("@records",recordsParameter); }); connection.Execute("Update_Records",spParams,commandType: CommandType.StoredProcedure);和
@H_502_2@connection.Execute("Update_Records",new Object[] { spParams,recordsParam },commandType: CommandType.StoredProcedure);两个调用过程,但传递一个空表参数(SELECT COUNT(*)FROM @records returns 0)
我似乎找不到Dapper.TVP的任何实际的文档或源代码,所以整个事情是非常混乱的,而.Execute()的第二个参数只是一个动态的,所以再也不会告诉我我能做什么不能传递给它.
有任何想法吗?
解决方法
我在手机上可能会误会这个问题,但这应该是:
@H_502_2@DataTable records = ...
connection.Execute("Update_Records",new {
currentYear = filter.currentYear,records
},commandType: CommandType.StoredProcedure
);