我想在带有自动编号列的表中插入一个新行。我的代码如下,我找不到如何与其他列同时插入自动数字列:
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