[OleDB] Daten in MS Access einfügen (INSERT) und letzte Insert-ID ermitteln

Dim DB_OleDBCommandInsert As New System.Data.OleDb.OleDbCommand
Dim DB_OleDBCommandSelect As New System.Data.OleDb.OleDbCommand

With DB_OleDBCommandInsert
    .CommandText = "INSERT INTO Users (Name) VALUES ('Horst')"
    .Connection = frmMain.DB_OleDBConnection ' Connection wurde in frmMain erzeugt
    .ExecuteNonQuery()
    .Dispose()
End With

Dim iID As Integer = -1 ' Platzhalter für die Insert-ID

'letzte Insert ID zurückholen, ab Provider=Microsoft.Jet.OLEDB.4.0 möglich
With DB_OleDBCommandSelect
    .CommandText = "SELECT @@IDENTITY"
    .Connection = frmMain.DB_OleDBConnection ' Connection wurde in frmMain erzeugt
    iID = .ExecuteScalar()
    .Dispose()
End With

DB_OleDBCommandInsert = Nothing
DB_OleDBCommandSelect = Nothing

Schreibe einen Kommentar