我是新来asp.net。我正在通过SQL命令执行一个简单的插入查询。现在我想检索此新插入行的唯一ID,并将其存储在数组中。我不知道如何解决这个问题。检索唯一ID为刚添加
我的代码
foreach (GridViewRow gvrow in gvuserdata.Rows)
{
string strQuery = "insert into vishalc.[Rental Table] (Car_Id_Reference, UserName, CarName,Price,startdate,enddate)" +
" values(@carid, @username, @carname,@price,@startdate,@enddate)";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.AddWithValue("@carid", gvrow.Cells[0].Text);
cmd.Parameters.AddWithValue("@username", gvrow.Cells[1].Text);
cmd.Parameters.AddWithValue("@carname", gvrow.Cells[2].Text);
cmd.Parameters.AddWithValue("@price", gvrow.Cells[3].Text);
cmd.Parameters.AddWithValue("@startdate", gvrow.Cells[4].Text);
cmd.Parameters.AddWithValue("@enddate", gvrow.Cells[5].Text);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
cmd.ExecuteNonQuery();
//I want to store ID for this row after insert command
}