我正在尝试调用一个接受表值参数的存储过程。
我正在遵循这个问题的指导方针,实施自定义参数类型:
internal class IntDynamicParam
{
string name;
IEnumerable<int> numbers;
public IntDynamicParam(string name,IEnumerable<int> numbers)
{
this.name = name;
this.numbers = numbers;
}
public void AddParameters(IDbCommand command)
{
var sqlCommand = (SqlCommand)command;
sqlCommand.CommandType = CommandType.StoredProcedure;
List<Microsoft.SqlServer.Server.SqlDataRecord> number_list = new List<Microsoft.SqlServer.Server.SqlDataRecord>();
// Create an SqlMetaData object that describes our table type.
Microsoft.SqlServer.Server.SqlMetaData[] tvp_definition = { new Microsoft.SqlServer.Server.SqlMetaData("n", SqlDbType.Int) };
foreach (int n in numbers)
{
// Create a new record, using the metadata array above.
Microsoft.SqlServer.Server.SqlDataRecord rec = new Microsoft.SqlServer.Server.SqlDataRecord(tvp_definition);
rec.SetInt32(0, n); // Set the value.
number_list.Add(rec); // Add it to the list.
}
// Add the table parameter.
var p = sqlCommand.Parameters.Add("@" +name, SqlDbType.Structured);
p.Direction = ParameterDirection.Input;
p.TypeName = "int_list_type";
p.Value = number_list;
}
}
并尝试按如下方式使用它:
var p = new DynamicParameters();
p.AddDynamicParams(new IntDynamicParam("@IDList", new int[] { 1000, 2000, 3000 }));
p.Add("@StartRow", startRow);
p.Add("@EndRow", endRow);
p.Add("@OrderByField", orderByField.ToString());
p.Add("@OrderBy", orderBy.ToString());
p.Add("@TotalRows", 0, dbType: DbType.Int32, direction: ParameterDirection.Output);
var v = cnn.Query<venue>(spName, p,
commandType: CommandType.StoredProcedure).ToList<IDBVenueLite>();
但是参数“ @IDList ”没有被传递。显然 AddDynamicParams 不是要走的路,有人可以帮我吗?