Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 使用PetaPoco将表值param传递给存储过程_C# 4.0_Stored Procedures_Sql Server 2008 R2_Petapoco_Table Valued Parameters - Fatal编程技术网

C# 4.0 使用PetaPoco将表值param传递给存储过程

C# 4.0 使用PetaPoco将表值param传递给存储过程,c#-4.0,stored-procedures,sql-server-2008-r2,petapoco,table-valued-parameters,C# 4.0,Stored Procedures,Sql Server 2008 R2,Petapoco,Table Valued Parameters,当我尝试使用PetaPoco调用SQLServer2008R2存储过程时 我的存储过程接受表值参数 如何使用表值param调用petapoco中的存储过程 以下是我想做的: var db = new PetaPoco.Database("repikaciskaBaza"); DataTable table = new DataTable(); DataColumn id = table.Columns.Add("id", type: typeof(Int32)); for (int i =

当我尝试使用PetaPoco调用SQLServer2008R2存储过程时

我的存储过程接受表值参数

如何使用表值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);
然后我得到一个例外

The table type parameter '@0' must have a valid type name.
当我定义我的参数时

            param.SqlDbType = SqlDbType.Structured;
            param.SqlValue = table;
            param.ParameterName = "@art_id";
            param.TypeName = SqlDbType.Structured.ToString();
然后我得到一个例外

列、参数或变量@0.:找不到结构化的数据类型

如何使用表值param定义
SqlParam
,以便将其与数据一起发送到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
根据,您应该使用:

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

SqlDbType.Structured
是实现这一点的关键。不要使用
DbType.Object

我这样做了,现在我有了不同的例外,但我不能说上面的方法不起作用。我无法在解决方案中使用命名参数。我最后只是做了一个@0。Query(“内部连接@0 rowIds ON…”,param.ToList()
var param = new SqlParameter();
param.SqlDbType = SqlDbType.Structured;