2014-02-07 189 views
0

我有一個medicalcentre和windowsadmin表,它們使用mcID彼此有關係。我有4個輸入的形式。輸入的mcType和mcCentre值中的兩個輸入值應該插入到medicalcentre表中,而其他兩個輸入值中的兩個輸入值,即winUsername和winPassword值應該輸入到windowsadmin表中。如何將值插入兩個表中?

這意味着我想要的是,我爲mcType,mcCentre,winUsername和winPassword輸入值。當單擊註冊時,mcCentre和mcType將被插入到medicalcentre表中並自動遞增一個mcID,該mcID將與輸入的winUsername和winPassword一起被插入到窗口管理表中。怎麼做?對於我的代碼如下。我只知道如何插入僅在medicalcentre表中的mcType和mcCentre。

我註冊MedicalCentre和WindowsAdmin帳戶形式

單選按鈕的名稱 - 醫院:rbHopistal

單選按鈕的名稱 - 診所:rbClinic

文本框的名字 - mcCentre:txtmcCentre

文本框名稱 - win用戶名:txtwin用戶名

文本框的名字 - winPassword:txtwinPassword

My Register MedicalCentre and WindowsAdmin account form

醫療中心和WINDOWSADMIN表 MEDICAL CENTRE AND WINDOWSADMIN table

錯誤試圖跟隨@Sergey Berezovskiy代碼。必須聲明標量變量「@mcID」。

Must declare the scalar variable "@mcID

我的表關係 My tables Relationship

private int AddMedicalCentreRecord() 
      { 
       int result = 0; 

       string strConnectionString = ConfigurationManager.ConnectionStrings["SACPConnection"].ConnectionString; 

       SqlConnection myConnect = new SqlConnection(strConnectionString); 

       String strCommandText = "INSERT MEDICALCENTRE(mcType, mcCentre) " 
        + " VALUES (@NewmcType,@NewmcCentre)"; 

       SqlCommand updateCmd = new SqlCommand(strCommandText, myConnect); 

       if (rbHopistal.Checked) 
        updateCmd.Parameters.AddWithValue("@NewmcType", "Hospital"); 
       else 
        updateCmd.Parameters.AddWithValue("@NewmcType", "Clinic"); 
       updateCmd.Parameters.AddWithValue("@NewmcCentre", txtmcCentre.Text); 

       myConnect.Open(); 

       result = updateCmd.ExecuteNonQuery(); 

       // STEP 5: Close 
       myConnect.Close(); 
       return result; 
      } 

錯誤試圖跟隨@Sergey Berezovskiy代碼。必須聲明標量變量「@mcID」。

private int AddMedicalCentreRecord() 
    { 
     int result = 0; 

     string strConnectionString = ConfigurationManager.ConnectionStrings["SACPConnection"].ConnectionString; 

     using (SqlConnection conn = new SqlConnection(strConnectionString)) 
     { 
      conn.Open(); 
      using (SqlTransaction transaction = conn.BeginTransaction()) 
      { 

       string insertCentreQuery = 
        @"INSERT MEDICALCENTRE (mcType, mcCentre) 
      VALUES (@NewmcType, @NewmcCentre) 
      SELECT SCOPE_IDENTITY()"; 

       SqlCommand insertCentreCmd = 
        new SqlCommand(insertCentreQuery, conn, transaction); 

       if (rbHopistal.Checked) 
        insertCentreCmd.Parameters.AddWithValue("@NewmcType", "Hospital"); 
       else 
        insertCentreCmd.Parameters.AddWithValue("@NewmcType", "Clinic"); 
       insertCentreCmd.Parameters.AddWithValue("@NewmcCentre", txtmcCentre.Text); 

       int mcID = Convert.ToInt32(insertCentreCmd.ExecuteScalar()); 

       string insertUserQuery = 
        @"INSERT WINDOWSADMIN (winUsername, winPassword, mcID) 
      VALUES (@userName, @password, @mcID)"; 
       SqlCommand insertUserCmd = 

        new SqlCommand(insertUserQuery, conn, transaction); 

       insertUserCmd.Parameters.AddWithValue("@userName", txtwinUsername.Text); 
       insertUserCmd.Parameters.AddWithValue("@password", txtwinPassword.Text); 

       insertUserCmd.ExecuteNonQuery(); 

       transaction.Commit(); 

       //conn.Open(); 

       //result = insertUserCmd.ExecuteNonQuery(); 

       conn.Close(); 
       return result; 
      } 
     }   


    } 

回答

1

您需要使用transaction - 開始事務,然後將數據插入到medicalcentre表中並獲取mcID。之後,將數據插入到windowsadmin表和交易事務中:

using(SqlConnection conn = new SqlConnection(strConnectionString)) 
{ 
    conn.Open(); 
    using(SqlTransaction transaction = conn.BeginTransaction()) 
    { 
     // execute first command 
     string insertCentreQuery = 
      @"INSERT MEDICALCENTRE (mcType, mcCentre) 
       VALUES (@NewmcType, @NewmcCentre) 
       SELECT SCOPE_IDENTITY()"; 

     SqlCommand insertCentreCmd = 
      new SqlCommand(insertCentreQuery, conn, transaction); 
     // ...provide command parameters 
     int mcID = Convert.ToInt32(insertCentreCmd.ExecuteScalar());   

     // execute second command 
     string insertUserQuery = 
      @"INSERT WINDOWSADMIN (winUsername, winPassword, mcID) 
       VALUES (@userName, @password, @mcID)"; 
     SqlCommand insertUserCmd = 
      new SqlCommand(insertUserQuery, conn, transaction); 
     // ...provide command parameters 
     insertUserCmd.ExecuteNonQuery(); 

     transaction.Commit(); 
    } 
} 
+0

omg是什麼?怎麼做? @Sergey Berezovskiy – Pony

+0

我添加了錯誤代碼,添加了錯誤圖像。什麼是錯的,我寫錯了嗎? @Sergey Berezovskiy – Pony

+0

@Jordjmax你應該在執行命令之前提供命令參數 –

相關問題