c#webform代码无法更新我的Access数据库

时间:2016-02-10 06:46:17

标签: c# oledb

大家好,这是一个名为Forgot password.aspx的页面上的代码 我需要用户提供他们的电子邮件,以便他们重置密码。

问题是程序确实运行了,但数据库中的记录没有更新,用户只是被重定向到登录页面

我做错了吗? 请帮忙!

protected void save_Password_Click(object sender, EventArgs e)
{


    OleDbConnection connect = new OleDbConnection();
    connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;
                                    Data Source=C:\Users\Student\Documents\websiteDatabase.accdb";


    OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT password FROM userInformation where emailAddress = '" + p_Email.Text + "'", connect);


    DataTable dt = new DataTable();


    adapter.Fill(dt);



    if ( dt.Rows.Count.ToString() == "1")
    {

        if(tb_newPassword.Text == tb_Confirm.Text)
        {

            //Opens the connection
            connect.Open();

            OleDbCommand cmd = connect.CreateCommand();   


            cmd.CommandText = "UPDATE userInformation SET password ='" + tb_Confirm.Text + "' WHERE emailAddress = '" + p_Email.Text  + "'";


            MessageBox.Show("Password changed successfully!");


            Response.Redirect("Sign In Page.aspx");

            //Closes the connection
            connect.Close();
        }
        else
        {

            lbl.Text = "Passwords do not match!";

        }
    }
    else
    {

        lbl.Text=" Change unsuccessful, please try again";

    }

}

2 个答案:

答案 0 :(得分:1)

你需要执行你的命令:

cmd.CommandText = "UPDATE userInformation SET password ='" + tb_Confirm.Text + "' WHERE emailAddress = '" + p_Email.Text  + "'";
cmd.ExecuteNonQuery()
MessageBox.Show("Password changed successfully!");

答案 1 :(得分:0)

我找到了解决方案!

            string email = p_Email.Text;
            string password  = tb_Confirm.Text

            try
                {
                        connect.Open();
                        OleDbCommand cmd = connect.CreateCommand();

                        cmd.CommandType = CommandType.Text;
                        cmd.CommandText = "UPDATE userInformation SET [password]= @password WHERE emailAddress= @email";
                        cmd.Parameters.AddWithValue("@password", tb_Confirm.Text);
                        cmd.Parameters.AddWithValue("@email", p_Email.Text);

                        cmd.ExecuteNonQuery();


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    connect.Close();
                }