Returns the ID generated for an AUTO_INCREMENT column by the previous command.
public long InsertId {get;}
'Declaration
Public ReadOnly Property InsertId As Long
Property Value
The ID generated for an AUTO_INCREMENT column.
In the following example an insert operation is performed assuming that DeptNo column in Test.Dept table has Autoincrement attribute. After running the query the value assigned to DeptNo for the inserted record is shown.
static void GetLastId(MySqlConnection myConnection)
{
MySqlCommand myCommand = new MySqlCommand("INSERT INTO Test.Dept (DeptNo, DName, Loc) VALUES (Null, 'AnotherOne', 'SomeWhere')", myConnection);
myConnection.Open();
try
{
myCommand.ExecuteNonQuery();
Console.WriteLine(myCommand.InsertId);
}
finally
{
myConnection.Close();
}
}
Public Sub GetLastId(ByVal myConnection As MySqlConnection)
Dim myCommand As New MySqlCommand("INSERT INTO Test.Dept (DeptNo, DName, Loc) VALUES (Null, 'AnotherOne', 'SomeWhere')", myConnection)
myConnection.Open()
Try
myCommand.ExecuteNonQuery()
Console.WriteLine(myCommand.InsertId)
Finally
myConnection.Close()
End Try
End Sub