C#

Asked • 07/19/19

How to get last inserted id?

I have this code: string insertSql = "INSERT INTO aspnet_GameProfiles(UserId,GameId) VALUES(@UserId, @GameId)"; using (SqlConnection myConnection = new SqlConnection(myConnectionString)) { myConnection.Open(); SqlCommand myCommand = new SqlCommand(insertSql, myConnection); myCommand.Parameters.AddWithValue("@UserId", newUserId); myCommand.Parameters.AddWithValue("@GameId", newGameId); myCommand.ExecuteNonQuery(); myConnection.Close(); } When I insert into this table, I have an auto_increment int primary key column called `GamesProfileId`, how can i get the last inserted one after this so I can use that id to insert into another table?

1 Expert Answer

By:

Benjamin H. answered • 08/02/20

Tutor
4.9 (18)

Full-stack Web Developer Specializing in C#/.NET

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.