ado*_*lot 8 stored-procedures table-valued-parameters sql-server-2008-r2 c#-4.0 petapoco
因为我试图使用PetaPoco调用SQL Server 2008 R2存储过程.
我的存储过程接受表值参数.
如何使用表值param调用petapoco中的存储过程?
这是我想要做的:
var db = new PetaPoco.Database("repikaciskaBaza");
DataTable table = new DataTable();
DataColumn id = table.Columns.Add("id", type: typeof(Int32));
for (int i = 0; i < 10;i++ )
{
DataRow row = table.NewRow();
row["id"] = i;
table.Rows.Add(row);
}
var param = new SqlParameter();
param.DbType = DbType.Object;
param.ParameterName = "@art_id";
param.SqlValue = table;
var lista = db.Query<pocoArts>(";exec dbo.test_sporc_param @0", param);
Run Code Online (Sandbox Code Playgroud)
这段代码给了我一个例外:
传入的表格数据流(TDS)远程过程调用(RPC)协议流不正确.
参数3("@ 0"):数据类型0x62(sql_variant)具有类型特定元数据的无效类型.
如果我设置了parametar ty值
param.SqlDbType = SqlDbType.Structured;
Run Code Online (Sandbox Code Playgroud)
然后我得到例外
The table type parameter '@0' must have a valid type name.
Run Code Online (Sandbox Code Playgroud)
当我定义我的param时
param.SqlDbType = SqlDbType.Structured;
param.SqlValue = table;
param.ParameterName = "@art_id";
param.TypeName = SqlDbType.Structured.ToString();
Run Code Online (Sandbox Code Playgroud)
然后我得到例外
列,参数或变量@ 0.:找不到数据类型结构化.
我如何定义SqlParam表值param,以便我可以将它发送到SQL Server?
方案:
var param = new SqlParameter();
param.SqlDbType = SqlDbType.Structured; // According to marc_s
param.SqlValue = table;
param.ParameterName = "@art_id";
param.TypeName = "dbo.typ_art_id"; // this is TYP from SQL Server database it needs to be equal to type defined in SQL Server not type of param
Run Code Online (Sandbox Code Playgroud)
根据有关表值参数的相关 MSDN 文档,您应该使用:
var param = new SqlParameter();
param.SqlDbType = SqlDbType.Structured;
Run Code Online (Sandbox Code Playgroud)
这SqlDbType.Structured是关键。不要使用DbType.Object.