Pass table value param to stored procedure using PetaPoco

隐身守侯 提交于 2019-12-04 20:31:37

问题


For while I am trying to call SQL Server 2008 R2 stored procedure using PetaPoco.

My stored procedure accepts a table valued parameter.

How I can call the stored procedure in petapoco with table value param?

Here what I am trying to do:

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);

This code gives me an exception :

The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect.
Parameter 3 ("@0"): Data type 0x62 (sql_variant) has an invalid type for type-specific metadata.

If I set parametar ty value

param.SqlDbType = SqlDbType.Structured;

Then I get exception like

The table type parameter '@0' must have a valid type name.

When I define my param like

            param.SqlDbType = SqlDbType.Structured;
            param.SqlValue = table;
            param.ParameterName = "@art_id";
            param.TypeName = SqlDbType.Structured.ToString();

Then I get exception

Column, parameter, or variable @0. : Cannot find data type Structured.

How I can define SqlParam with table valued param so I can send it whit data to SQL Server?

Solution:

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

回答1:


According to the relevant MSDN documentation on table-valued parameter, you should use:

var param = new SqlParameter();
param.SqlDbType = SqlDbType.Structured;

The SqlDbType.Structured is the key to this. Don't use DbType.Object.



来源:https://stackoverflow.com/questions/17019412/pass-table-value-param-to-stored-procedure-using-petapoco

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!