I have an C# application utilizing a SQL Server database. After inserting a row, I need the ID
of the row that was created. I tried select max(id) from mytable
, but the result from this is incorrect if another user has inserted a row in the meantime.
This is my code:
public string ConnectionString = @"Data Source=.;Initial Catalog=n_hesabdata;Integrated Security=True";
public void ExecuteNonQuery(string CommandText)
{
try {
connection.ConnectionString = ConnectionString;
connection.Open();
command.CommandText = CommandText;
command.Connection = connection;
command.ExecuteNonQuery();
command.Dispose();
connection.Close();
} catch (Exception error) {
connection.Close();
}
}
ExecuteNonQuery("insert into Orders (Ctm_phone, Ctm_FullName) VALUES (" + Ctm_phone.text + ", N'" + Ctm_name.Text + "')");
How do I get the ID
of the freshly inserted row?