并发冲突:UpdateCommand 影响了具有自动编号字段的预期 1 条记录中的 0 条

发布于 2024-11-26 20:31:08 字数 792 浏览 6 评论 0原文

我正在使用 Visual Studio 2008、.Net 3.5 的新数据层以及 SQLite 编写一个小型 C# 协会会员应用程序。

我使用各种向导来生成主窗体,其中有一个 BindingNavigator 和一个 BindingSource。这个想法是,表单显示 1 条记录,您可以更新记录,并使用 BindingNavigator 控件导航到其他记录、添加记录、删除记录等。

我希望将当前记录写回数据库每当用户导航到不同的记录(或关闭表单)时。

成员记录中有一个自动编号字段(我打算与应用程序中其他地方的其他表建立关系)。

这一切都有效,除非我添加新记录。新记录已保存,但此后任何更新数据的尝试都会在标题中给出错误消息。

我意识到这是因为保存数据时自动编号字段尚未添加到数据集的本地副本中。但我不知道在哪里放置一些代码来检索自动编号 ID 并将其填充到新记录中。

我可以将代码放在为数据表生成的代码的 Insert 方法中,但是下次我更改设计器中的任何内容时,我的更改将会丢失,而且我不确定 Insert 方法是否实际被调用(在那里放置断点不会似乎没有停止)。

我在Google上搜索答案,发现建议我在数据表设计器的高级配置设置中选中“更新后刷新”复选框,但这并没有改变生成的代码,也没有任何效果(当我返回时一看,复选框又被取消勾选了)。

补充问题 - 哪里是阅读有关 DataBindingNavigator、DataBindingSource 和新生成的数据类如何组合在一起的概述的最佳位置?

最后,如果您看到我另一个类似的问题,我深表歉意。我本可以发誓我昨天问过一个,但我在这里找不到它,所以我假设它从未被发布。

I am writing a small C# society membership app using Visual Studio 2008, and the new data layer with .Net 3.5, along with SQLite.

I used the various wizards to generate my main form, which has a BindingNavigator and a BindingSource on it. The idea is that the form shows 1 record, and you can update the record, and use the BindingNavigator controls to navigate to the other records, add records, delete records, etc.

I would like the current record to be written back to the database whenever the user navigates to a different record (or closes the form).

The member records have an autonumber field in them (I intend to have relationships with other tables elsewhere in the app).

That all works, except when I add a new record. The new record is saved, but any attempt to update data thereafter gives the error message in the title.

I realise this is because the autonumber field has not been added to the local copy of the dataset when the data is saved. But I am at a loss as to where to put some code to retrieve the autonumber id and stuff it in the new record.

I could put the code in the Insert method of the generated code for the data table, but my changes would be lost next time I changed anything in the designer, plus I am not sure the Insert method actually gets called (putting a breakpoint there doesn't seem to stop).

I Googled for an answer, and found a suggestion that I check the "Refresh after update" checkbox in the advanced configuration settings in the data table designer, but that did not change the generated code, or have any effect (and when I went back to look, the checkbox was unticked again).

Supplementary question - where is the best place to read an overview of how DataBindingNavigator, DataBindingSource, and the new generated data classes all fit together?

Finally, apologies if you see another, similar question from me. I could have sworn I asked one yesterday, but I can't find it here, so I am assuming it never got posted.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文