且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获取数据库当前记录的主键

更新时间:2023-01-27 10:43:04

我'建议阅读以下内容:

如何:在将记录插入Access时检索身份值数据库使用Visual Basic .NET [ ^ ]

检索身份或自动值 [ ^ ]


我在这个帖子中问了原始问题,想要发布我在另一个论坛上找到的解决方案,以便它可以帮助其他有类似需求的人。我收到的上一个答案是我的问题太过牵扯。我正在寻找一些简单的东西,比如下面的代码。



我原本试图找到特定记录的主键,所以我可以用它来编写代码转到该记录并将其显示在表单上。但是这里是通过提供该记录的RecordID来转到Access数据库中的特定记录的代码。对于我的数据库表,RecordID是表中的一列,对每条记录都是唯一的,并由用户输入。但是,它不是主键。我的数据库中的表名称为:tblAssetData。我使用此代码允许用户在数据库中查找特定记录,然后将其显示在表单上。从按钮单击事件调用代码,然后将RecordID作为整数参数提供给sub。



 ' 转到表中的特定记录:tblAssetData  
Sub GoToRecord(RecordID As Integer
Me .TblAssetDataBindingSource.Position = Me .TblAssetDataBindingSource.Find( AssetID,RecordID)
结束 Sub


I am trying to find the vb.net code needed to find the Primary Key number of the current row of the database table that the user is on when they save the changes they have made to the table. I am using an Access database that is bound to a form in my application. My connection to the database is OLE.db rather than ADO. I have searched the vb.net forums and purchased 4 books on vb.net, but I can't find the answer. I will be greatly appreciative of any help with the vb.net code to find the Primary Key number of the current row/record of the database table.

I'd suggest to read these:
HOW TO: Retrieve the Identity Value While Inserting Records into Access Database By Using Visual Basic .NET [^]
Retrieving Identity or Autonumber Values[^]


I asked the original question in this thread and want to post the solution I found on another forum so that it might help others with a similar need. The previous answer that I received to my question was way too involved. I was looking for something simple, like the code below.

I was originally trying to find the primary key of a specific record so I could use it to write code to go to that record and display it on the form. But here is the code to go to a specific record in an Access database by supplying the RecordID of that record. For my database table, the RecordID is a column in the table that is unique to each record and is entered by the user. It is not the Primary Key, however. The name of the table in my database is named: tblAssetData. I use this code to allow the user to find a specific record in the database and then display it on the form. The code is called from a button click event, and then the RecordID is supplied to the sub as an integer argument.

'Go To a Specific Record In Table: tblAssetData
Sub GoToRecord(RecordID As Integer)
    Me.TblAssetDataBindingSource.Position = Me.TblAssetDataBindingSource.Find("AssetID", RecordID)
End Sub