1

将 TVP 传递给存储过程时出现错误

我的代码是

SqlConnection con=new SqlConnection();
con.Open();

SqlCommand cmd=new SqlCommand("SPName",con);

DataTable dataTable = new DataTable("vpTableGuid");

dataTable.Columns.Add("id1", typeof(Guid));
dataTable.Columns.Add("rowNum", typeof(Int32));

dataTable.Rows.Add(Guid.NewGuid(),1);
dataTable.Rows.Add(Guid.NewGuid(),2);
dataTable.Rows.Add(Guid.NewGuid(),3);

SqlParameter param = new SqlParameter("@evaluatorList", dataTable);                        
param.SqlDbType = SqlDbType.Structured;
param.TypeName = "dbo.vpTableGuid";

command.Parameters.Add(param);

command.ExecuteNonQuery();

上面的代码抛出一个错误:

插入到表变量上不允许的标识列。
表值参数“@evaluatorList”的数据不符合参数的表类型。

4

1 回答 1

0

You cannot set the identity column manually in SQL.

Define your primary key column in SQL as such:

id1 UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY

Remove this line of code:

dataTable.Columns.Add("id1", typeof(Guid));

And change your vpTableGuid definition to not include the id1 column.

于 2012-10-05T13:18:48.307 回答