使用表值参数更新数据表

时间:2014-11-06 22:25:14

标签: c# sql-server table-valued-parameters

我是表值参数的新手。我有两个表:Business(父)和Businessdetail(child)。有一列:business_id,它是业务表的主键和businessdetail的外键。两个表之间的关系:

ds.Relations.Add(new DataRelation("Business_BusinessDetail", ds.Tables["Business"].Columns["Business_ID"],ds.Tables["BusinessDetail"].Columns["Business_ID"]));

现在,在使用表值参数插入父表时,我想在插入子表之前更新主键。我的代码:

//父

                            SqlParameter tvpParamparent = insertCmd.Parameters.AddWithValue("@Business", newBusinessDS.Tables["Business"]);//Needed TVP
                            tvpParamparent.SqlDbType = SqlDbType.Structured;
                            insertCmd.ExecuteNonQuery();-->does not update business_id

//子

SqlParameter tvpParamchild = insCmdChild.Parameters.AddWithValue("@BusinessDetail", newBusinessDS.Tables["BusinessDetail"]);
                                tvpParamchild.SqlDbType = SqlDbType.Structured;/
                                insCmdChild.ExecuteNonQuery();-->it throws an error:"The MERGE statement conflicted with the FOREIGN KEY constraint "FK_BusinessDetail_Business""

1 个答案:

答案 0 :(得分:0)

不要使用" @"参数名称中的字符