C# TableAdapter 和 DataSet 更新多个表
我是 C# 新手,需要一些帮助。
我只想通过下面的代码更新会话表中的行。
dbDataSet db = new dbDataSet();
SessionTableAdapter sessionTableAdapter = new SessionTableAdapter();
sessionTableAdapter.Fill(db.Session);
var session = db.Session.Where(s => s.idSession.Equals(new Guid("{0F0B0E1A-950E-4BCE-9C68-6D1387EDAC90}"))).SingleOrDefault();
session.endTime = DateTime.Now;
sessionTableAdapter.Update(db.Session);
我在“sessionTableAdapter.Update(db.Session)”处收到错误
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: The record cannot be deleted or changed because table 'PaidLog' includes related records.
如何修复此错误?
I'm new in C# and I need some help.
I just want to update row in Session table by code below.
dbDataSet db = new dbDataSet();
SessionTableAdapter sessionTableAdapter = new SessionTableAdapter();
sessionTableAdapter.Fill(db.Session);
var session = db.Session.Where(s => s.idSession.Equals(new Guid("{0F0B0E1A-950E-4BCE-9C68-6D1387EDAC90}"))).SingleOrDefault();
session.endTime = DateTime.Now;
sessionTableAdapter.Update(db.Session);
I got an error at "sessionTableAdapter.Update(db.Session)"
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: The record cannot be deleted or changed because table 'PaidLog' includes related records.
How to fix this error ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误消息说明了一切。 Session表与PaidLog有外键关系。您必须首先更新该表中的子行。
请参阅这篇MSDN 上的文章,了解“更新使用 TableAdapter 的数据集中的两个相关表” 部分
或者您可能想要使用 TableAdapterManager
The error message says it all. Session table has foreign key relation with PaidLog. You will have to update child rows from this table first.
See this article on MSDN for more details in the "Updating Two Related Tables in a Dataset with a TableAdapter" section
Or you might want to use TableAdapterManager
解决了!
起初,我只勾选“强制引用完整性”。
但我尝试勾选级联更新相关字段并且它有效!
我不知道为什么它有效。
有谁知道这件事的原因吗?
顺便说一句,谢谢马希普。
Solved !
At first that I just only tick Enforce Referential Integrity.
But I try to tick Cascade Update Related Fields and It's works !
I don't know why it work.
Anyone know the reason about this thing?
By the way, thanks to Maheep.