It seems that there is way to get the AutoIncrement value from the database during the excution of Update method, but there is no way to avoid another query to the database.
You can use the RowUpdated
event raised by the OleDbDataAdapter
after a row has been updated (or inserted).
We need to use the "SELECT @@IDENTITY" command supported by Access, but this cannot be effective if we don't work on the same connection used by the Update thus the only option is to use the RowUpdated
event where the connection is still open
' Need to have a OleDbCommandBuilder to build the InsertCommand for the OleDbDataAdapter
Sub loadCars()
....
daCar = New OleDbDataAdapter(sql, oledbConn)
Dim builder = new OleDbCommandBuilder(daCar)
daCar.InsertCommand = builder.GetInsertCommand
daCar.Fill(dsDataSet, "car")
End Sub
' In the update code
AddHandler daCar.RowUpdated, New OleDbRowUpdatedEventHandler(AddressOf NewRowInserted)
daCar.Update(dsDataSet.Tables("car"))
' The event handler is called for any row inserted, updated or deleted
Sub NewRowInserted(sender as Object, e as OleDb.OleDbRowUpdatedEventArgs)
' Check if we are working for a row with the proper state
If e.Row.RowState = DataRowState.Added Then
Dim cmd = new OleDbCommand("SELECT @@IDENTITY",e.Command.Connection)
Dim newID = DirectCast(cmd.ExecuteScalar(), Integer)
Console.WriteLine(newID)
End If
End Sub