相关疑难解决方法(0)

如何获取最后插入的ID?

我有这个代码:

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();
}
Run Code Online (Sandbox Code Playgroud)

当我插入这个表时,我有一个auto_increment int主键列调用GamesProfileId,如何在此之后获取最后一个插入的列,以便我可以使用该id插入另一个表?

c# sql sql-server

170
推荐指数
3
解决办法
20万
查看次数

标签 统计

c# ×1

sql ×1

sql-server ×1