使用类型化 DataSet 和 DataGridView 最后插入的记录 ID 问题
我将以下内容用于一个简单的数据库应用程序:
- SQL Server Compact Edition .sdf 文件作为数据库,具有 int 主键 ID。
- 类型化的DataSet和BindingSource作为数据访问层,
- DataGridView用于显示数据。
我的问题是,我在处理最后插入的记录/行 ID 时遇到问题。 当我使用导航器组件的追加按钮向 datagridview 添加一行时,新记录/行的 ID 为 -1。 即使使用 TableAdapter.Update() 将数据保存到数据库后,它仍然是 -1。 我知道我可以使用带有@@identity或scope_identity()的单独查询来获取最后一个ID,但这听起来不太正确,您必须使用另一个查询来手动更新您的数据。 我在这里错过了什么吗? 保存到数据库并获取刚刚插入的记录的 ID 后,是否有自动更新数据的方法?
另外,我在数据集设计器->表适配器配置->高级窗口中看到了“刷新数据表”选项,但由于某种原因被禁用。 但我不知道它是否相关..
我将不胜感激任何帮助..
I'm using following for a simple database application:
- SQL Server Compact Edition .sdf file as database, with int primary key IDs.
- Typed DataSet and BindingSource as data access layer
- DataGridView for displaying data.
My problem is, I'm having trouble with the last inserted record/row ID. When I add a row to datagridview, using Append button of a navigator component, the ID of the new record/row is -1. It's still -1 even after save data to database using TableAdapter.Update(). I know I can get last ID using a seperate query with @@identity or scope_identity() but it doesn't sound right that you just have to use another query to update your data, manually at that. Am I missing something here? Is there an automatic way to update your data after saving to database and getting the ID of the record you just inserted?
Also, I saw a "refresh the datatable" option in dataset designer->table adapter configuration->advanced window, but it's disabled for some reason. But I don't know if it's related..
I'd appreciate any help with this..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
感谢两位的回答。 我已经找到了问题的根源,不幸的是没有我喜欢的解决方案。 问题出在 SQL Server Compact Edition 中。 由于它不支持存储过程或多活动结果集 (MARS),因此您无法在一个查询中运行多个 SQL。 因此,“刷新数据表”选项被禁用,如果不禁用,则会将 ID 值更新为实际值。 我要将主键 ID 值从 int 更改为 Guid,并处理 DataGridView 的 RowInit 事件,并手动为新行提供 Guid ID。
Thanks for the both answers. I've found the source of the problem, unfortunately there's no solution to my liking. The problem is in SQL Server Compact Edition. Since it doesn't support stored procedures or Multiple Active Result Set (MARS) you can't run more than one SQL in one query. Therefore "refresh the datatable" option is disabled, which would update ID value with actual value if it would not disabled. I'm gonna change my primary key ID valumn from int to Guid and handle RowInit event of the DataGridView and give a Guid ID to new rows manually.
我通常对 DTO 执行的操作与下面的 vb 类似
我的存储过程与下面类似
只是为了澄清基类中的 1 个衬垫的作用(使用企业库仅供参考)
What I typically do with my DTO is something similar to the vb below
With my stored procedure similar to the below
Just to clarify what my 1 liner in the base class does (using the enterprise library fyi)
您还可以尝试将标识列声明为存储过程中的 OUTPUT 参数。
You can also try declaring your identity column as an OUTPUT parameter in your stored procedure.
我在 Microsoft SQL Server Compact Edition 的文档中找到了这一点:
返回最后插入的标识:
SELECT @@IDENTITY
或者
选择 @@IDENTITY 作为“TEMPNAME”
I found this in documentation to Microsoft SQL Server Compact Edition:
return last inserted identity:
SELECT @@IDENTITY
or
SELECT @@IDENTITY AS 'TEMPNAME'
将 IdentitySeed 和 IdentityIncrement 设置为 1,一切正常。
set IdentitySeed and IdentityIncrement to 1 and evereything works.
我认为更新数据库后,只要您依赖数据库为新插入的记录自动生成键,您就必须在 gridview 上调用 databind() 。
I think you're going to have to call databind() on your gridview after you update the database, as long as you're depending on the database to auto-generate a key for the newly inserted record.