0

我有两个通过外键和主键约束相关的表。Patient 表中的 Visit_Number 必须存在于 Visit 表中。在我的代码中定义创建我的连接字符串的两个实例,以便我可以使用一个实例插入记录:VisitNumber 在访问首先表,然后另一个实例在患者表中插入记录,相信绳子是从上到下运行的。但事实并非如此。我仍然得到外键约束错误:

Error Number:547 
Error MessageThe INSERT statement conflicted with the FOREIGN KEY constraint "Patient_Vist_FK".
 The conflict occurred in database "TestDB", table "dbo.Visit", column 'Visit_Number'.
The statement has been terminated.On line Number: 1

意味着代码按我的预期运行。请问您有更好的方法吗,为什么我的代码不起作用:

protected void btn_save_Click(object sender, EventArgs e)
    {




        string connect = System.Configuration.ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection con = new SqlConnection(connect);
        SqlConnection con2 = new SqlConnection(connect);
        string visitnumber = txtVistNumber.Text.ToString();
        string insert_statement = "Insert into Patient(Patient_Number,FirstName,LastName,Gender,Tribe,Date_Of_Birth,Visit_Number)"
            + "Values(@Patient_Number,@FirstName,@LastName,@Gender,@Tribe,@Date_Of_Birth,@Visit_Number)";
        string insert_stament2 = "Insert into Visit(Visit_Number)"
            + "Values(@Visit_Number)";

        SqlCommand cmd = new SqlCommand(insert_statement, con);
        SqlCommand cmd2 = new SqlCommand(insert_stament2, con2);

        cmd2.Parameters.AddWithValue("@Visit_Number", txtVistNumber.Text);
        cmd.Parameters.AddWithValue("@Patient_Number",txtpatientNum.Text);
        cmd.Parameters.AddWithValue("@FirstName",txtfirstName.Text);
        cmd.Parameters.AddWithValue("@LastName",txtlastname.Text);
        cmd.Parameters.AddWithValue("@Gender", drl_gender.SelectedValue);
        cmd.Parameters.AddWithValue("@Tribe",DropDownList1.Text);
        cmd.Parameters.AddWithValue("@Date_Of_Birth", val_age.Text);
        cmd.Parameters.AddWithValue("@Visit_Number", txtVistNumber.Text);





        try
        {
            using (con)
            {
                con.Open();
                int count = cmd.ExecuteNonQuery();
                if (count > 0)
                {
                    Response.Write("<script language=javascript>alert('Record Sucessfully Inserted!');</script>");
                    //Success_Message.Text = "Record inserted";
                    txtpatientNum.Text = String.Empty;
                    txtfirstName.Text = String.Empty;
                    txtlastname.Text = String.Empty;
                    txtVistNumber.Text = String.Empty;
                    DropDownList1.Text = String.Empty;
                    val_age.Text = String.Empty;

                }


            }

        }
        catch (SqlException ex)
        {


            {
                VisitError_Message.Text = "Error Number:" + ex.Number.ToString() + " Error Message" + ex.Message + "On line Number" + ": " + ex.LineNumber;
            }



        }
        catch (NullReferenceException nullexception)
        {
            VisitError_Message.Text = "Error Occurred, Error Type:" + nullexception.GetType().ToString() + "Error Message:" + nullexception.Message;
        }
        catch (DllNotFoundException dllexception)
        {
            VisitError_Message.Text = dllexception.GetType().ToString() + dllexception.Message;
        }

        finally
        {
            con.Close();
        }

    }
4

1 回答 1

1

你没有执行你的cmd2,你必须在cmd2中执行插入Visit_Number然后执行你的cmd,你可以测试这个代码

         using (con2)
        {
            con2.Open();
            cmd2.ExecuteNonQuery();
        }

然后你可以执行你的cmd

        using (con)
        {
            con.Open();
            int count = cmd.ExecuteNonQuery();
        }

如果您愿意,您可以使用相同的连接来完成工作

    string connect = System.Configuration.ConfigurationManager.ConnectionStrings["db_connection"].ToString();
    SqlConnection con = new SqlConnection(connect);
    string visitnumber = txtVistNumber.Text.ToString();
    string insert_statement = "Insert into Patient(Patient_Number,FirstName,LastName,Gender,Tribe,Date_Of_Birth,Visit_Number)"
        + "Values(@Patient_Number,@FirstName,@LastName,@Gender,@Tribe,@Date_Of_Birth,@Visit_Number)";
    string insert_stament2 = "Insert into Visit(Visit_Number)"
        + "Values(@Visit_Number)";

        using(con)
        {
            con.open;

            SqlCommand cmd2 = new SqlCommand(insert_stament2, con);

            cmd2.Parameters.AddWithValue("@Visit_Number", txtVistNumber.Text);
            cmd2.ExecuteNonQuery();

            SqlCommand cmd = new SqlCommand(insert_statement, con);

            cmd.Parameters.AddWithValue("@Visit_Number", txtVistNumber.Text);
            cmd.Parameters.AddWithValue("@Patient_Number",txtpatientNum.Text);
            cmd.Parameters.AddWithValue("@FirstName",txtfirstName.Text);
            cmd.Parameters.AddWithValue("@LastName",txtlastname.Text);
            cmd.Parameters.AddWithValue("@Gender", drl_gender.SelectedValue);
            cmd.Parameters.AddWithValue("@Tribe",DropDownList1.Text);
            cmd.Parameters.AddWithValue("@Date_Of_Birth", val_age.Text);
            cmd.Parameters.AddWithValue("@Visit_Number", txtVistNumber.Text);
            cmd.ExecuteNonQuery();      
    }
于 2013-03-13T13:39:37.180 回答