2013-03-13 69 views
0

我有兩個相關的外國和主鍵約束。患者表中的Visit_Number必須存在於Visit表中。在我的代碼中定義創建我的連接字符串的兩個實例,以便我可以使用一個實例插入記錄:VisitNumber首先在訪問表中,然後在另一個實例中將記錄插入患者表中,並相信該電線從上到下運行。但事實並非如此。我仍然得到外鍵約束errror:以什麼順序C#執行sqlstatement?

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(); 
     } 

    } 
+1

其餘的代碼執行? – Akrem 2013-03-13 13:32:51

+0

我不明白pls – kombo 2013-03-13 13:34:00

+0

方法'btn_save_Click'缺少代碼。你能發表其餘的代碼嗎? – 2013-03-13 13:35:33

回答

1

你不excuting你cmd2的輸入,你必須執行在CMD2插入Visit_Number然後EXCUTE你的CMD,您可以測試這個代碼

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

那麼你可以EXCUTE您的CMD

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

,如果你想

,你可以用相同的Connexion公司做的工作
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();  
    }