使用表值参数更新datatable

本文关键字:更新 datatable 参数 值参 | 更新日期: 2023-09-27 18:29:33

我是表值参数的新手。我有两个表:Business(父表)和Businessdetail(子表)。有一列: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""

使用表值参数更新datatable

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