使用类型化 DataSet 和 DataGridView 最后插入的记录 ID 问题

发布于 2024-07-11 06:15:41 字数 571 浏览 7 评论 0原文

我将以下内容用于一个简单的数据库应用程序:

  • 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 技术交流群。

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

发布评论

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

评论(6

万水千山粽是情ミ 2024-07-18 06:15:41

感谢两位的回答。 我已经找到了问题的根源,不幸的是没有我喜欢的解决方案。 问题出在 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.

苏大泽ㄣ 2024-07-18 06:15:41

我通常对 DTO 执行的操作与下面的 vb 类似

Public Sub InsertSupplier(ByVal SupplierObject As Supplier) Implements ISupplierRepository.InsertSupplier
SupplierObject.SupplierID = MyBase.ExecuteNonQueryWithReturnValue("usp_Northwind_InsertSupplier", SupplierObject.CompanyName)
End Sub

我的存储过程与下面类似

USE [Northwind]
GO
/****** Object:  StoredProcedure [dbo].[usp_Northwind_InsertSupplier]    Script Date: 1/1/2009 10:59:01 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_Northwind_InsertSupplier]
@CompanyName nvarchar(40)
AS
INSERT INTO dbo.Suppliers
VALUES (@CompanyName)

RETURN @@Identity

只是为了澄清基类中的 1 个衬垫的作用(使用企业库仅供参考)

Public Function ExecuteNonQueryWithReturnValue(ByVal storedProcedureName As String, ByVal ParamArray parameterValues() As Object) As Object Implements Interfaces.IRepository(Of T).ExecuteNonQueryWithReturnValue
    Dim Output As Object
    Dim db As Database = DatabaseFactory.CreateDatabase()
    Dim dbCommand As Common.DbCommand = db.GetStoredProcCommand(storedProcedureName, parameterValues)
    db.ExecuteNonQuery(dbCommand)
    Output = CType(db.GetParameterValue(dbCommand, "@RETURN_VALUE"), Object)
  Return Output
End Function

What I typically do with my DTO is something similar to the vb below

Public Sub InsertSupplier(ByVal SupplierObject As Supplier) Implements ISupplierRepository.InsertSupplier
SupplierObject.SupplierID = MyBase.ExecuteNonQueryWithReturnValue("usp_Northwind_InsertSupplier", SupplierObject.CompanyName)
End Sub

With my stored procedure similar to the below

USE [Northwind]
GO
/****** Object:  StoredProcedure [dbo].[usp_Northwind_InsertSupplier]    Script Date: 1/1/2009 10:59:01 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_Northwind_InsertSupplier]
@CompanyName nvarchar(40)
AS
INSERT INTO dbo.Suppliers
VALUES (@CompanyName)

RETURN @@Identity

Just to clarify what my 1 liner in the base class does (using the enterprise library fyi)

Public Function ExecuteNonQueryWithReturnValue(ByVal storedProcedureName As String, ByVal ParamArray parameterValues() As Object) As Object Implements Interfaces.IRepository(Of T).ExecuteNonQueryWithReturnValue
    Dim Output As Object
    Dim db As Database = DatabaseFactory.CreateDatabase()
    Dim dbCommand As Common.DbCommand = db.GetStoredProcCommand(storedProcedureName, parameterValues)
    db.ExecuteNonQuery(dbCommand)
    Output = CType(db.GetParameterValue(dbCommand, "@RETURN_VALUE"), Object)
  Return Output
End Function
半岛未凉 2024-07-18 06:15:41

您还可以尝试将标识列声明为存储过程中的 OUTPUT 参数。

CREATE PROCEDURE YourSP
(@ID int OUTPUT, ...

You can also try declaring your identity column as an OUTPUT parameter in your stored procedure.

CREATE PROCEDURE YourSP
(@ID int OUTPUT, ...
街角卖回忆 2024-07-18 06:15:41

我在 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'

只为守护你 2024-07-18 06:15:41

将 IdentitySeed 和 IdentityIncrement 设置为 1,一切正常。

set IdentitySeed and IdentityIncrement to 1 and evereything works.

居里长安 2024-07-18 06:15:41

我认为更新数据库后,只要您依赖数据库为新插入的记录自动生成键,您就必须在 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.

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