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

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

问题描述

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

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

但是有并发异常:


System.Data.DBConcurrencyException未处理Message =并发
违例:UpdateCommand影响预期1条记录的0。


那么我应该如何将绑定源更改提交给DB呢?






POST EDIT
我启动程序时收到此异常,然后单击DataGridView中的第二行,然后单击第三行,此时程序提升一个并发异常。希望他的帮助得到这个问题更详细。



提前感谢!

解决方案

这样的东西?

  try 
{
aBindingSource.EndEdit();
dbDataSetA.GetChanges();
aTableAdapter.Update(dbDataSetA.Accounts);
}
catch(DBConcurrencyException exc)
{
MessageBox.Show(原始数据已更改,请重做更新);
aTableAdapter.Fill(dbDataSetA);
}

然后根据需要重新分配dbDataSetA作为DataSource,用户必须再次输入数据。 / p>

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!

解决方案

Something like this?

try
{
   aBindingSource.EndEdit();
   dbDataSetA.GetChanges();
   aTableAdapter.Update(dbDataSetA.Accounts);   
}
catch (DBConcurrencyException exc)
{
   MessageBox.Show("original data changed, please redo updates");
   aTableAdapter.Fill(dbDataSetA);
}

Then reassign dbDataSetA as DataSource if needed and user has to enter data again.

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

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