如何正确提交 bindingSource 更改到源数据库? [英] How to properly commit bindingSource changes to source DB?

查看:27
本文介绍了如何正确提交 bindingSource 更改到源数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了 DataGridView 和其他 UI 组件以允许用户编辑 SQLite DB 中的数据.但是这些更改(即使它们在应用程序中正确显示)不会保存到数据库中.我试过这段代码

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);   

但是有并发异常:

System.Data.DBConcurrencyException 未处理 Message=Concurrency违规:UpdateCommand 影响了预期的 1 条记录中的 0 条.

System.Data.DBConcurrencyException was unhandled Message=Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.

那么,伙计们,我应该如何将 bindingsource 更改提交到 DB?

So how should I commit bindingsource changes to DB, guys?

后编辑我在启动程序时遇到了这个异常,然后单击 DataGridView 中的第二行,然后单击第三行,这次程序引发了并发异常.希望他的帮助能更详细地解决这个问题.

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.

提前谢谢各位!

推荐答案

遇到了同样的问题.诀窍是,一旦你更新表,你应该清空"GetChanges().您可以通过调用方法 AcceptChanges() 来做到这一点.所以...

Had the same problem. Trick is, once you update table, you should "empty" GetChanges(). You do that by calling method AcceptChanges(). So...

aBindingSource.EndEdit();
dbDataSetA.GetChanges();
aTableAdapter.Update(dbDataSetA.Accounts); 
dbDataSetA.AcceptChanges();

只要是同样的问题,它应该可以工作.

It should work, provided it is the same problem.

这篇关于如何正确提交 bindingSource 更改到源数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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