我尝试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;
}