2013-07-25 104 views
0

我是新成員。另外,我正試圖在2個不同的表中添加一條記錄(customerinfo & studentinfo)。我的代碼在下面,但它僅將文本框字段記錄到StudentInfo表中。我應該如何處理它同時將記錄放入兩個表格中?如何將記錄添加到Access數據庫的TWO表中?

感謝

protected void btnRegister_Click(object sender, EventArgs e) 
    { 
     OleDbConnection mDB = new OleDbConnection(); 
     mDB.ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0;Data source=" 
      + Server.MapPath("~/App_Data/webBase.accdb"); 
     mDB.Open(); 
     Type csType = this.GetType(); 
     //check to ensure that UserId keyed in is not being used by other Customers 
     OleDbCommand cmd; 
     OleDbCommand cmd1; 
     OleDbDataReader rdr; 
     OleDbDataReader rdr1; 

     string strSQLSelect = "SELECT sUserId FROM studentInfo ORDER BY sUserId"; 
     string strSQLSelect1 = "SELECT cUserId FROM customerInfo ORDER BY cUserId"; 
     cmd1 = new OleDbCommand(strSQLSelect1, mDB); 
     cmd = new OleDbCommand(strSQLSelect, mDB); 
     rdr = cmd.ExecuteReader(); 
     rdr1 = cmd1.ExecuteReader(); 
     this.txtPassword.Attributes.Add("value", this.txtPassword.Text); 

// insert new record 
       string strSQLInsert = "INSERT INTO " 
       + "studentInfo (sUserId,sPassword,sName,sAddress,sTel,sEmail,sLevel, sLevel2)" 
       + "VALUES(@uid,@pw,@name,@addr,@em,@tel,@lvl,@lvl2)"; 
       ClientScript.RegisterStartupScript(csType, "Successful!", scriptSuccessNewAccount); 
       cmd = new OleDbCommand(strSQLInsert, mDB); 
       cmd.Parameters.AddWithValue("@uid", txtUserId.Text); 
       cmd.Parameters.AddWithValue("@pw", txtPassword.Text); 
       cmd.Parameters.AddWithValue("@name", txtName.Text); 
       cmd.Parameters.AddWithValue("@addr", txtAddress.Text); 
       cmd.Parameters.AddWithValue("@em", txtEmail.Text); 
       cmd.Parameters.AddWithValue("@tel", txtTel.Text); 
       cmd.Parameters.AddWithValue("@lvl", DropDownList1.Text); 
       cmd.Parameters.AddWithValue("@lvl2", DropDownList2.Text); 




       string strSQLInsert1 = "INSERT INTO " 
    + "customerInfo (cUserId,cPassword,cName,cAddress,cEmail,cTel,cCountry)" 
    + "VALUES(@uid,@pw,@name,@addr,@em,@tel,@country)"; 
       ClientScript.RegisterStartupScript(csType, "Successful!", scriptSuccessNewAccount); 
       cmd1 = new OleDbCommand(strSQLInsert1, mDB); 
       cmd1.Parameters.AddWithValue("@uid", txtUserId.Text); 
       cmd1.Parameters.AddWithValue("@pw", txtPassword.Text); 
       cmd1.Parameters.AddWithValue("@name", txtName.Text); 
       cmd1.Parameters.AddWithValue("@addr", txtAddress.Text); 
       cmd1.Parameters.AddWithValue("@em", txtEmail.Text); 
       cmd1.Parameters.AddWithValue("@tel", txtTel.Text); 
       cmd1.Parameters.AddWithValue("@country", txtCountry.Text); 

       cmd.ExecuteNonQuery(); 
       mDB.Close(); 

回答

1

它看起來像你缺少

cmd1.ExecuteNonQuery() 

爲其他表。

+0

謝謝!這工作。是我的疏忽,我會在大約6分鐘內給出解決方案! –

相關問題