0

我是新会员。此外,我正在尝试将记录添加到 2 个不同的表(客户信息和学生信息)中。我的代码在下面,但它只将文本框字段记录到 StudentInfo 表中。我应该如何将记录同时放入 2 个表中?

谢谢

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

1 回答 1

1

你好像不见了

cmd1.ExecuteNonQuery()

对于另一张桌子。

于 2013-07-25T14:54:34.590 回答