Skip to main content
1 of 3

Is it safe to aquire an id from a newly inserted entry in MS Sql Database like this? (c#)

In an MSSQL Database, i have automatically increasing ids for every entry. Is it safe to aquire the id of the newly inserted entry like this? I am worried that, if at the exact same time another entry gets inserted, i could end up with the wrong id..

I have the following c# code directly after my INSERT INTO - query getting executed:

                    query = "SELECT ID FROM subscriptions ORDER BY ID DESC";
                    SqlCommand command = new SqlCommand(query, openCon);
                    SqlDataReader reader = command.ExecuteReader();
                    reader.Read();

                    int id = reader.GetInt32(0);

                    result.Add("success", "true");

                    result.Add("id", id.ToString());