vb.net MS Access 使用 SQL 查询将带有自动编号主键列的记录插入 table

vb.net MS Access insert record with auto number primary key column into table with SQL query

我想在带有自动编号列的 table 中插入一个新行。我的代码如下,我找不到如何与其他列同时插入自动编号列:

pth = My.Application.Info.DirectoryPath

Dim SQL = "INSERT INTO approved (id, word, approveds) VALUES (@idd, @word, @approval)"

Using Con As New OleDbConnection("Provider=Microsoft.ace.oledb.12.0; Data Source=" & pth & "\database.mdb; User Id=; Password=;")
    Dim Cmd As New OleDbCommand(SQL, Con)

    Cmd.Parameters.Add("@idd", OleDb.OleDbType.VarChar).Value = @@identity
    Cmd.Parameters.Add("@word", OleDb.OleDbType.VarChar).Value = RichTextBox1.SelectedText
    Cmd.Parameters.Add("@approval", OleDb.OleDbType.VarChar).Value = "YES"

    Con.Open()
    Cmd.ExecuteNonQuery()
End Using

关键字@@identity无效;正确的方法是什么?

如果您将 SQL 列设置为标识自动递增,则无需传递 id 参数,插入时它会自动工作。

删除以下代码:

Cmd.Parameters.Add("@idd", OleDb.OleDbType.VarChar).Value = @@identity

附录:

当然也可以用:

Dim SQL = "insert into approved (word, approveds) VALUES (@word, @approval)"

这有效:

 pth = My.Application.Info.DirectoryPath



            Dim SQL = "insert into approved (word, approveds) VALUES (@word, @approval)"
            Using Con As New OleDbConnection("Provider=Microsoft.ace.oledb.12.0; Data Source=" & pth & "\database.mdb; User Id=; Password=;")
                Dim Cmd As New OleDbCommand(SQL, Con)

                Cmd.Parameters.Add("@word", OleDb.OleDbType.VarChar).Value = RichTextBox1.SelectedText
                Cmd.Parameters.Add("@approval", OleDb.OleDbType.VarChar).Value = "YES"
                Con.Open()
                Cmd.ExecuteNonQuery()
            End Using