如何获取数据库当前记录的主键 [英] How do I get the primary key of the current record of the database

查看:119
本文介绍了如何获取数据库当前记录的主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找用户在保存对表所做的更改时查找用户所在数据库表当前行的主键号所需的vb.net代码。我正在使用绑定到我的应用程序中的表单的Access数据库。我与数据库的连接是OLE.db而不是ADO。我搜索过vb.net论坛并在vb.net上购买了4本书,但我找不到答案。我将非常感谢vb.net代码的任何帮助,以找到数据库表的当前行/记录的主键号。

解决方案

我'建议阅读以下内容:

如何:在将记录插入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


这篇关于如何获取数据库当前记录的主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆