我试图调用接受一个表值参数的存储过程。
我下面的准则这一问题,实现自定义的参数类型:
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是不要去,谁能帮助我的出路何在?