从 MS Access 数据库检索最后插入的 ID

发布于 2025-01-08 12:10:58 字数 1828 浏览 0 评论 0原文

我有以下一组子程序代码。它使用包含表单中提供的数据将一行插入 MSAccess 数据库。我想要做的是获取此添加记录的 ID 号,以便可以将其设置为成功添加时调用的窗口的属性。我尝试查找此内容,但我得到了有关 @@IDENTITY 的信息,但它使用了完全不同的连接方式。

Private Sub CreateTournament_Click(sender As System.Object, e As System.EventArgs) Handles CreateTournament.Click
    ' TODO: Check the form for errors, or blank values.
    ' Create the tournament in the database, add the values where needed. Close the form when done.

    Dim cn As OleDbConnection
    Dim cmd As OleDbCommand
    Dim dr As OleDbDataReader
    Dim icount As Integer
    Dim str As String

    Try
        cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\Master.mdb'")
        cn.Open()
        str = "insert into Tournaments (SanctioningID,TournamentName,TournamentVenue,TournamentDateTime,TournamentFirstTable,Game,Format,OrganizerID) values(" _
            & CInt(SanctioningIDTxt.Text) & ",'" & Trim(TournamentNameTxt.Text) & "','" & _
            "1" & "','" & EventDateTimePck.Value & "','" & TableFirstNumberNo.Value & "','" & GameList.SelectedIndex & "','" & FormatList.SelectedIndex & "','" & Convert.ToInt32(ToIDTxt.Text) & "')"

        'string stores the command and CInt is used to convert number to string
        cmd = New OleDbCommand(Str, cn)
        icount = cmd.ExecuteNonQuery
        MessageBox.Show(icount)
        'displays number of records inserted
    Catch ex As Exception
        MessageBox.Show(ex.ToString)
    End Try

    Me.Close()

    Dim n As New TournamentWindow ' Open a new Tournament window if everything is successful
    n.TournID = Counter '<< This should be set to the ID of the most recently inserted row
    n.Show(HomeForm)'Invoke the form and assign "HomeForm" as it's parent.

End Sub

I have the following set of code for a Sub program. It's inserting a row into a MSAccess Database using data provided in the containing form. What I would like to do is grab the ID number of this added record so that it can be set for a property of a window that is invoked when successfully added. I tried looking this up but I get something about @@IDENTITY but it's using an entirely different way of connecting.

Private Sub CreateTournament_Click(sender As System.Object, e As System.EventArgs) Handles CreateTournament.Click
    ' TODO: Check the form for errors, or blank values.
    ' Create the tournament in the database, add the values where needed. Close the form when done.

    Dim cn As OleDbConnection
    Dim cmd As OleDbCommand
    Dim dr As OleDbDataReader
    Dim icount As Integer
    Dim str As String

    Try
        cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\Master.mdb'")
        cn.Open()
        str = "insert into Tournaments (SanctioningID,TournamentName,TournamentVenue,TournamentDateTime,TournamentFirstTable,Game,Format,OrganizerID) values(" _
            & CInt(SanctioningIDTxt.Text) & ",'" & Trim(TournamentNameTxt.Text) & "','" & _
            "1" & "','" & EventDateTimePck.Value & "','" & TableFirstNumberNo.Value & "','" & GameList.SelectedIndex & "','" & FormatList.SelectedIndex & "','" & Convert.ToInt32(ToIDTxt.Text) & "')"

        'string stores the command and CInt is used to convert number to string
        cmd = New OleDbCommand(Str, cn)
        icount = cmd.ExecuteNonQuery
        MessageBox.Show(icount)
        'displays number of records inserted
    Catch ex As Exception
        MessageBox.Show(ex.ToString)
    End Try

    Me.Close()

    Dim n As New TournamentWindow ' Open a new Tournament window if everything is successful
    n.TournID = Counter '<< This should be set to the ID of the most recently inserted row
    n.Show(HomeForm)'Invoke the form and assign "HomeForm" as it's parent.

End Sub

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

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

发布评论

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

评论(1

居里长安 2025-01-15 12:10:58

假设 Tournaments 表中有一个自动增量列,您可以执行“SELECT @@IDENTITY”来获取最后插入的记录的 id。

顺便说一句,SanctioningIDTxt.Text 是唯一的吗?如果是这样,你就不能用它吗?

Assuming you have an auto increment column in the Tournaments table, you can do a "SELECT @@IDENTITY" to get the id of the last inserted record.

BTW, is the SanctioningIDTxt.Text unique? If so, can't you use that?

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