2014-02-08 79 views
1

我有一個medicalcentre表和windowadmin表,它們使用mcID彼此有關係。我首先顯示mcType和mcCentre存在於medicalcentre表格中,然後使用outer join將windowsadmin表中的winusername字段和winPassword字段顯示爲gridview。我想要實現的是使用刪除功能刪除記錄。但我得到錯誤,看到第三張圖片。如何從兩個表中刪除值?

我windowsadmin和medicalcentre表 enter image description here

我windowsadmin和medicalcentre表關係 enter image description here

我的形式 enter image description here

我的錯誤 enter image description here

private void LoadMedicalCentreRecords() 
     { 

      //retrieve connection information info from App.config 
      string strConnectionString = ConfigurationManager.ConnectionStrings["SACPConnection"].ConnectionString; 
      //STEP 1: Create connection 
      SqlConnection myConnect = new SqlConnection(strConnectionString); 
      //STEP 2: Create command 
      string strCommandText = "SELECT cen.mcid, mcType, mcCentre, win.winUsername, win.winPassword FROM MEDICALCENTRE AS cen"; 
      strCommandText += " LEFT OUTER JOIN WINDOWSADMIN as win on cen.mcid = win.mcid"; 
      MedicalCentreAdapter = new SqlDataAdapter(strCommandText, myConnect); 

      //command builder generates Select, update, delete and insert SQL 
      // statements for MedicalCentreAdapter 
      //SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(MedicalCentreAdapter); 
      // Empty Employee Table first 
      MedicalCentre.Clear(); 
      // Fill Employee Table with data retrieved by data adapter 
      // using SELECT statement 
      MedicalCentreAdapter.Fill(MedicalCentre); 

      // if there are records, bind to Grid view & display 
      if (MedicalCentre.Rows.Count > 0) 
       grdMc.DataSource = MedicalCentre; 
     } 




private int DeleteMedicalCentreRecord() 
     { 
      // no row in GridView selected 
      if (currentRow == null) 
      { 
       return 0; 
      } 
      //retrieve connection information info from App.config 
      string strConnectionString = ConfigurationManager.ConnectionStrings["SACPConnection"].ConnectionString; 
      //STEP 1: Create connection 
      SqlConnection myConnect = new SqlConnection(strConnectionString); 
      //STEP 2: Create command 
      string strCommandText = "DELETE FROM WINDOWSADMIN WHERE [email protected]; DELETE FROM MEDICALCENTRE WHERE [email protected];"; 
      SqlCommand deleteCmd = new SqlCommand(strCommandText, myConnect); 

      // Cell - contains employee ID 
      int winID = Convert.ToInt32(currentRow.Cells[0].Value); 
      int mcID = Convert.ToInt32(currentRow.Cells[0].Value); 
      deleteCmd.Parameters.AddWithValue("@WINID", winID); 
      deleteCmd.Parameters.AddWithValue("@MCID", mcID);   
      //// STEP 3: open connection and retrieve data by calling ExecuteReader 
      myConnect.Open(); 
      //STEP 4: execute command 
      int result = deleteCmd.ExecuteNonQuery(); 
      // STEP 5: Close 
      myConnect.Close(); 
      return result; 
     } 
+0

您是否碰巧在ON DELETE RESTRICT有一個外鍵? –

+0

我真的這麼認爲,我該如何檢查它? erm在我使用外連接之前,我只能刪除medicalcentre表中的字段,但是在外連接windowsadmin表後,還爲windowsadmin字段寫入了刪除代碼。錯誤出來了@Vlad Schnakovszki – Pony

+1

您可以顯示創建FK_WINDOWSADMIN_MEDICALCENTER外鍵的SQL代碼嗎? – Steve

回答

1

我認爲你需要改變你的DELETE命令

string strCommandText = @"DELETE FROM WINDOWSADMIN WHERE [email protected]; 
          DELETE FROM MEDICALCENTRE WHERE [email protected];"; 

這確保具有您要刪除將從刪除與醫療中心releationship一次勝利用戶WindowsAdmin表

換句話說,您可以刪除WINDOWSADMIN表中的每個記錄,但是如果WIN中有記錄,則無法刪除MEDICALCENTRE表中的記錄DOWSADMIN表仍然參考您要刪除的醫療記錄中的記錄