如何正确地将绑定源更改提交到源数据库?

发布于 2024-12-25 10:23:51 字数 532 浏览 2 评论 0原文

我设置了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦幻的心爱 2025-01-01 10:23:51

像这样的东西吗?

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

然后,如果需要,将 dbDataSetA 重新分配为数据源,并且用户必须再次输入数据。

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.

萌︼了一个春 2025-01-01 10:23:51

有同样的问题。技巧是,一旦更新表,您应该“清空”GetChanges()。您可以通过调用方法 AcceptChanges() 来完成此操作。所以...

aBindingSource.EndEdit();
dbDataSetA.GetChanges();
aTableAdapter.Update(dbDataSetA.Accounts); 
dbDataSetA.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.

辞慾 2025-01-01 10:23:51

您可以将适配器与命令生成器结合使用,如下所示:

DataTable table = new DataTable();
var adapter = new SqlDataAdapter("SELECT * FROM ...", con)) 

//Load the dataTable and the bound datagridView
adapter.Fill(table);



    using (new SqlCommandBuilder(adapter))
    {
      //When done you can update the database using the Command builder
      adapter.Update(table);
    }

You could use an Adapter in combination with a command builder, something like this :

DataTable table = new DataTable();
var adapter = new SqlDataAdapter("SELECT * FROM ...", con)) 

//Load the dataTable and the bound datagridView
adapter.Fill(table);



    using (new SqlCommandBuilder(adapter))
    {
      //When done you can update the database using the Command builder
      adapter.Update(table);
    }
爱你是孤单的心事 2025-01-01 10:23:51

也许太大了,但你可以尝试使用 Transaction 只需阅读这篇文章,可能会有所帮助:

事务sqlite

Maybe oversize but can you try to use Transaction just read this Post, could be helpful :

Transactional sqlite

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文