在C#,sql中更新语句

时间:2013-06-09 08:57:52

标签: c# mysql

从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>");
        }
    }

1 个答案:

答案 0 :(得分:2)

您尝试两次调用ExecuteNonQuery来更新密码两次。在第一次调用ExecuteNonQuery时,它会返回1未收集的内容,当您第二次调用ExecuteNonQuery时,它会返回0,因此它会始终执行else 1}}部分。

进行以下更改 -

    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>");
    }