我尝试在插入查询后放置Selct SCOPE_IDENTITY;
来获取表中最后一次插入的id。
问题在于,当我尝试在标签中显示插入游客的ID时 - 没有任何反应。 (仅执行插入查询);
string insertSQL = "INSERT INTO Tourist (Excursion_ID, Excursion_date_ID, Name_kir,Midname_kir, Lastname_kir)";
insertSQL += "VALUES (@Excursion_ID, @Excursion_date_ID, @Name_kir,@Midname_kir, @Lastname_kir);Select SCOPE_IDENTITY()";
string connectionString = "Data Source = localhost\\SQLExpress;Initial Catalog=excursion;Integrated Security=SSPI";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(insertSQL, con);
cmd.Parameters.AddWithValue("@Excursion_ID", Convert.ToInt32(mynew2));
cmd.Parameters.AddWithValue("@Excursion_date_ID", Convert.ToInt32(mynewnewstring));
cmd.Parameters.AddWithValue("@Name_kir",tx888.Text);
cmd.Parameters.AddWithValue("@MidName_kir",tx888_1.Text);
cmd.Parameters.AddWithValue("@LastName_kir",tx888_2.Text);
int added = 0;
try
{
con.Open();
added = (int)cmd.ExecuteScalar();
if (added > 0)
{
lblproba.Text+=added.ToString();
}
}
catch (Exception ex)
{
//lblResult.Text = ex.Message;
}