DataRow自引用时如何级联主键更新? [英] How to cascade update of primary key when DataRow is self-referencing ?

查看:78
本文介绍了DataRow自引用时如何级联主键更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。


我有一个只有1个表的数据集,名为T.该表有2列:列"ID" (主键)和列"refID"。我有一个外键约束(具有级联密钥更新和删除),其中"T.refID"是指。 "T.ID"指的是"T.ID"。
换句话说,我有一个循环关系。我的ID是自动递增的(-1),我的OracleDataAdapter的INSERT命令确保从Oracle序列生成一个唯一的ID,然后使用Oracle"RETURNING INTO"返回到该行。
条款。这一切都很好。


当我向T添加2行时,请说t1和t2,并且t1指t2,反之亦然(即t1.refID = t2.ID和t2.refID = t1 .ID)然后我需要调用Update(T)2次才能在Oracle数据库中找到正确的值。这很好,因为我通常
做一个"while(T.HasChanges()){Update(T); }" ;.


然而,当我只添加一行t并自己进行行引用(即t.refID = t.ID)然后我看到更新后我看到了t.ID从Oracle序列生成,并且数据库t.refID中的t.refID = t.ID  仍为
-1!因此,DataRow与Oracle表中的行没有相同的值。我找到的解决方法是再次调用t.SetModified然后更新(T)以强制将t.refID保存到数据库中。


问题:我该怎样才能避免打电话给"t.SetModified"。 ?


谢谢,


Danny


解决方案

嗨Danny,


>>问题  :我怎样才能避免打电话? ; t.SetModified" ?


根据以下文档,当行的某个元素发生更改时。更新后,行状态将更改为"已修改"并从数据表中检索并更改值。


https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/ dataset-datatable-dataview / row-states-and-row-versions


您还可以通过以下代码获取相关行。


< pre class ="prettyprint"> foreach(Table.Rows中的DataRow dr)
{
Console.WriteLine(" {0}",dr.RowState);
}


祝你好运,


张龙


Hello.

I have a DataSet with just 1 table called T. The table has 2 columns : column "ID" (primary key) and column "refID". I have a foreign key constraint (with cascading key updates and deletes) whereby "T.refID" refers to "T.ID". In other words, I have a cyclic relation. My ID is auto-incremented (by -1) and the INSERT command of my OracleDataAdapter makes sure to generate a unique ID from an Oracle sequence, which is then returned into the row using the Oracle "RETURNING INTO" clause. All this works fine.

When I add 2 rows to T, say t1 and t2 and have t1 refer to t2 and vice versa (ie. t1.refID = t2.ID and t2.refID = t1.ID) then I need to call Update(T) 2 times to end up with the right values in the Oracle database. This is fine because I typically do a "while (T.HasChanges()) { Update(T); }".

However, when I add just ONE row t and make the row reference herself (ie. t.refID = t.ID) then I see after the Update I see the t.ID generated from the Oracle sequence and also t.refID = t.ID but in the database t.refID is still -1 ! So the DataRow does not have the same values as the row in the Oracle table. The workaround I found is to call t.SetModified and then Update(T) once more to force saving of the t.refID into the database.

Question : how can I avoid having call "t.SetModified" ?

Thanks,

Danny

解决方案

Hi Danny,

>>Question : how can I avoid having call "t.SetModified" ?

Based on the following document, when some element of the row has been changed. the rows state will be changed to 'Modified' after you updatechanges and retrieve it from datatable and change the values.

https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/dataset-datatable-dataview/row-states-and-row-versions

you could also get related rows via the following code.

 foreach (DataRow dr in table.Rows)
{
     Console.WriteLine("{0}",dr.RowState);
}

Best regards,

Zhanglong


这篇关于DataRow自引用时如何级联主键更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆