База данных MS-Access
Table: myTable
Field1: ID (COUNTER, which is autoincrement in Access)
Field2: SURNAME (VARCHAR)
Field3: NAME (VARCHAR)
Я хочу получить идентификатор , возвращаемый при каждой вставке строки Есть ли способ? (Я использую VB.Net)
EDIT:
Решение с использованием 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