有2个数据表T1和T2,它们之间有2个关系. T1包含3列T1ID,T2ID,T1Name. T1ID是T1的PK列,并且是自动编号身份. T1中的T2ID col可以包含空值,但T2的FK col可以是唯一的. T2包含3列T2ID,T1ID和T2Name. T2ID是T2的PK列,也是自动编号. T1ID在T2中不是可以为null的col,它是T1的FK col.这些表在这里以2种方式关联. 1对多,0或1对1.我可以在ADO级别插入或更新数据,但是当此修改后的数据集在sql数据库中发送以进行更新时,sql服务器会抛出外键约束异常.
There are 2 data tables, T1 and T2, which have 2 relations between them. T1 contains 3 columns T1ID, T2ID, T1Name. The T1ID is PK column for T1 and is auto-number identity. T2ID col in T1 can contain nulls but unique and FK col for T2. T2 contains 3 columns T2ID, T1ID and T2Name. T2ID is the PK col for T2 and is also auto-number. T1ID is not null-able col in T2 and is the FK col for T1. Here these tables are related in 2 ways. 1 to many and 0 or 1 to 1. I can insert or update data in ADO level but when this modified dataset is sending for updating in the sql database , the foreign key constraint exception is thrown by the sql server.
//ADO Level DataRow t1NewRow = ds.Tables["T1"].NewRow(); t1NewRow.BeginEdit(); t1NewRow["T1Name"] = somevalue; t1NewRow.EndEdit(); DataRow t2NewRow = ds.Tables["T2"].NewRow(); t2NewRow.BegigEdit(); t2NewRow["T1ID"] = t1NewRow["T1ID"]; t2NewRow["T2Name"] = somevalue; t2NewRow.EndEdit(); ds.Tables["T1"].Rows.Add(t1NewRow); ds.Tables["T2"].Rows.Add(t2NewRow); t1NewRow.BeginEdit(); t1NewRow["T2ID"] = t2NewRow["T2ID"]; t1NewRow.EndEdit();[修改:刚刚修复了准悬挂修饰符]
[Modified: just fixed the quasi-dangling modifier]
推荐答案提交更改后,您可能需要刷新行以确保由ADO是正确的(应该是没有并发问题的可能性),或者是完全生成的(您的约束是在代码中的某个位置定义的,还是以某种方式反映出来的?). 此处一遍 [ ^ ]有关如何使您的密钥同步并在更新后刷新. 干杯. You''ll likely need to refresh your rows after you commit changes to make sure that the key generated by ADO is correct (it should be if there are no possibilities of concurrency problems) or is generated at all (are your constraints defined somewhere in code, or reflected somehow?). Here''s a walk through[^] on how to get your keys sync''d up and refreshed after the update. Cheers.
更多推荐
重新关联表
发布评论