Re: Getting the ID of a newly added record (C#)
Hello Greg,
You could use the property LastInsertedId from MySqlCommand class, see this test case
public void testcase_lastinsertedid()
{
executeSQL("DROP TABLE IF EXISTS testing");
executeSQL("CREATE TABLE testing(id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, data VARCHAR(10))");
var str= "ONE";
using (var command = Connection.CreateCommand())
{
command.CommandText = "INSERT INTO testing(data) VALUES(@data); ";
command.Parameters.AddWithValue("@data", str);
command.ExecuteNonQuery();
Assert.Equal(1, command.LastInsertedId);//first record
str = "TWO";
command.ExecuteNonQuery();
Assert.Equal(2, command.LastInsertedId);//secod record
}
}
Best Regards.
Gustavo Cuatepotzo
Subject
Written By
Posted
Re: Getting the ID of a newly added record (C#)
March 13, 2020 04:46PM
Sorry, you can't reply to this topic. It has been closed.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.