2

从我从 textbox1、textbox2 获得的输入中,我想检查 DB,如果给定的用户名和密码组合可用,那么我想用 textbox3 中可用的输入更新密码。以下是我的代码,但似乎我的更新语句无法正常工作,请帮助我更正它...

     protected void Button1_Click(object sender, EventArgs e)
    {
        MySqlConnection connection = new MySqlConnection("server=localhost; database=e-learningsystem; uid=root; password=123;port=3307;");
        connection.Open();
        try
        {

            MySqlCommand cmd1 = new MySqlCommand("UPDATE lecturer Set Password= '" + TextBox3.Text + "' WHERE UserName='" + TextBox1.Text + "' AND Password='" + TextBox2.Text + "'", connection);
            cmd1.ExecuteNonQuery();
            if (cmd1.ExecuteNonQuery() > 0)
            {
                Response.Write(@"<script language='javascript'>alert('Your Password Has Been Changed successfully!, Congratulations!')</script>");
            }
            else
            {
                Response.Write(@"<script language='javascript'>alert('Your Password Has Not Been Changed successfully!, Sorry!')</script>");
            }
                connection.Close();
        }
        catch (Exception ex)
        {
            Response.Write(@"<script language='javascript'>alert(ex.Message)</script>");
        }
    }
4

1 回答 1

2

您试图通过调用ExecuteNonQuery两次来更新密码。在第一次调用ExecuteNonQuery它时会返回1未收集的数据,而第二次调用ExecuteNonQuery它时会返回0,因此它将始终执行该else部分。

进行以下更改 -

    int updateCount = cmd1.ExecuteNonQuery();
    if (updateCount > 0)
    {
        Response.Write(@"<script language='javascript'>alert('Your Password Has Been Changed successfully!, Congratulations!')</script>");
    }
    else
    {
        Response.Write(@"<script language='javascript'>alert('Your Password Has Not Been Changed successfully!, Sorry!')</script>");
    }
于 2013-06-09T09:46:08.877 回答