MS-Access数据库
Table: myTable
Field1: ID (COUNTER, which is autoincrement in Access)
Field2: SURNAME (VARCHAR)
Field3: NAME (VARCHAR)
每当我插入一行时,我都希望获取ID 。有办法吗? (我使用的是VB.Net)
编辑: 使用SELECT @@ IDENTITY
的解决方案'Execute insert statement and return identity (autoincrement ID field)
Public Function executeInsertGetIdentity(ByVal insertStatement As String) As Integer
' Execute insert
Dim myOleDbCommand As OleDbCommand
myOleDbCommand = New OleDbCommand(insertStatement, connection)
If myOleDbCommand.ExecuteNonQuery() < 1 Then Return 0
' Return identity
Dim myOleDbCommand2 As OleDbCommand
myOleDbCommand2 = New OleDbCommand("SELECT @@IDENTITY", connection)
Dim myOleDbDataReader As OleDbDataReader = myOleDbCommand.ExecuteReader()
myOleDbDataReader.Read()
executeInsertGetIdentity = myOleDbDataReader.Item(0).ToString()
myOleDbDataReader.Close()
End Function
答案 0 :(得分:2)