0

这是我尝试做的事情

在 button_click 上,我从文本框中读取值并将它们插入到数据库中。作为旅游人数,例如可能有两个或三个使用 ExecuteScalar 的游客;我得到了插入的游客的身份证!

公共无效cmdInsert_OnClick(对象发送者,EventArgs e){

        if (Page.IsValid)
        {
            string numtourist = (string)Session["tourist_number"];
            for (int i = 0; i < Int32.Parse(numtourist); i++)
            {
                TextBox tx888 = (TextBox)FindControl("txtNameK" + i.ToString());
                TextBox tx888_1 = (TextBox)FindControl("txtMidNameK" + i.ToString());
                TextBox tx888_2 = (TextBox)FindControl("txtLastNameK" + i.ToString());

                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 @@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;
                try
                {
                    con.Open();
                    added = (int)cmd.ExecuteScalar();


                    lblproba.Text = "";
                    Tourist.Add(added);
                    lblproba.Text += Tourist.Count();


                }
                catch (Exception ex)
                {
                    lblproba.Text += ex.Message;


                }
                finally
                {
                    con.Close();

                }
            }
            createReservation();
        }
    }

我调用 CreateReservationFunction 并使用已进行预订的用户的 ID 创建一个新的预订。使用 SELECT IDENTITY 我尝试获取预留的 RESERVATION_ID,在这里我得到异常“无法评估表达式,因为代码已优化或本机框架位于调用堆栈顶部”。所以我想知道这个异常是否与以下事实有共同之处,net 但实体框架

公共无效createReservation(){

        string insertSQL = "Insert INTO RESERVATIONS (User_ID) values (@User_ID)  SELECT @@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("@User_ID", 9);


        try
        {
            con.Open();
            string added = cmd.ExecuteScalar().ToString();
            createTouristReservation(added);


        }
        catch (Exception ex)
        {

            lblproba.Text+= ex.Message; 

        }




    }
4

1 回答 1

0

不要使用 @@IDENTITYbutSCOPE_IDENTITY并在插入和选择之间添加分号。

string insertSQL = @"INSERT INTO Tourist (Excursion_ID, Excursion_date_ID, Name_kir,Midname_kir, Lastname_kir)
                     VALUES (@Excursion_ID, @Excursion_date_ID, @Name_kir,@Midname_kir, @Lastname_kir) 
                    ;SELECT CAST(scope_identity() AS int)";
于 2013-09-27T08:26:45.247 回答