如何正确地将绑定源更改提交到源数据库?
我设置了 DataGridView 和其他 UI 组件,以允许用户编辑 SQLite DB 中的数据。但是这些更改(即使它们在应用程序中正确显示)不会保存到数据库中。我已经尝试过这段代码
aBindingSource.EndEdit();
dbDataSetA.GetChanges();
aTableAdapter.Update(dbDataSetA.Accounts);
,但存在并发异常:
System.Data.DBConcurrencyException 未处理 Message=Concurrency 违规:UpdateCommand 影响了预期 1 条记录中的 0 条。
那么我应该如何将绑定源更改提交到数据库呢?
帖子编辑 当我启动程序,然后单击 DataGridView 中的第二行,然后单击第三行时,出现此异常,这次程序引发并发异常。希望他的帮助使这个问题更详细。
预先感谢各位!
I setup DataGridView and other UI components to allow user to edit data from SQLite DB. But these changes (even they are properly shows in application) doesn't saves to DB. I have tried this code
aBindingSource.EndEdit();
dbDataSetA.GetChanges();
aTableAdapter.Update(dbDataSetA.Accounts);
but there are concurrency exception:
System.Data.DBConcurrencyException was unhandled Message=Concurrency
violation: the UpdateCommand affected 0 of the expected 1 records.
So how should I commit bindingsource changes to DB, guys?
POST EDIT
I got this exception when I start the program, then click on second row in DataGridView and then on third and this time program raise a concurrency exception. Hope his help get this issue more detailed.
Thanks in advance, guys!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像这样的东西吗?
然后,如果需要,将 dbDataSetA 重新分配为数据源,并且用户必须再次输入数据。
Something like this?
Then reassign dbDataSetA as DataSource if needed and user has to enter data again.
有同样的问题。技巧是,一旦更新表,您应该“清空”
GetChanges()
。您可以通过调用方法AcceptChanges()
来完成此操作。所以...它应该有效,只要它是同样的问题。
Had the same problem. Trick is, once you update table, you should "empty"
GetChanges()
. You do that by calling methodAcceptChanges()
. So...It should work, provided it is the same problem.
您可以将适配器与命令生成器结合使用,如下所示:
You could use an Adapter in combination with a command builder, something like this :
也许太大了,但你可以尝试使用 Transaction 只需阅读这篇文章,可能会有所帮助:
事务sqlite
Maybe oversize but can you try to use Transaction just read this Post, could be helpful :
Transactional sqlite