Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/590123620971732992
deleted 2 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

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

In an MSSQL Databasedatabase, iI have automatically increasing idsIDs for every entry. Is it safe to aquireacquire the idID of the newly inserted entry like this? I am worried that, if at the exact same time another entry gets inserted, iI could end up with the wrong id.ID.

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..

Acquiring an ID from a newly inserted entry in an MSSQL database

In an MSSQL database, I have automatically increasing IDs for every entry. Is it safe to acquire 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.

added 1050 characters in body
Source Link
string query = "INSERT INTO subscriptions (polygon,...) VALUES (@polygon,...)";
                    SqlDataAdapter cmd = new SqlDataAdapter();  
                    using (SqlCommand querySaveSubscription = new SqlCommand(query))
                    {
                        querySaveSubscription.Connection = openCon;
                        // Create Poly out of given geodata values
                        var point = SqlGeography.Point(Double.Parse(lat), Double.Parse(lon), 4326);
                        SqlGeography poly = point.BufferWithTolerance(Double.Parse(radius) * 1000, 0.01, true);

     

                        var param = new SqlParameter(@"polygon", poly);
                        param.UdtTypeName = "Geography";

                        querySaveSubscription.Parameters.Add(param);
        ...
                 


                        openCon.Open();
                        querySaveSubscription.ExecuteNonQuery();
string query = "INSERT INTO subscriptions (polygon,...) VALUES (@polygon,...)";
                    SqlDataAdapter cmd = new SqlDataAdapter();  
                    using (SqlCommand querySaveSubscription = new SqlCommand(query))
                    {
                        querySaveSubscription.Connection = openCon;
                        // Create Poly out of given geodata values
                        var point = SqlGeography.Point(Double.Parse(lat), Double.Parse(lon), 4326);
                        SqlGeography poly = point.BufferWithTolerance(Double.Parse(radius) * 1000, 0.01, true);

     

                        var param = new SqlParameter(@"polygon", poly);
                        param.UdtTypeName = "Geography";

                        querySaveSubscription.Parameters.Add(param);
        ...
                 


                        openCon.Open();
                        querySaveSubscription.ExecuteNonQuery();
Source Link

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());