更新声明无效

时间:2016-07-23 14:15:39

标签: c# asp.net

protected void Button6_Click(object sender, EventArgs e)
{
    Random rnd = new Random();
    string resetpassword = rnd.Next(5000, 100000).ToString();
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HealthDBContext"].ConnectionString);
    conn.Open();
    string reset = "UPDATE Users SET" + " Password=@pass" + " WHERE UserName=@user";
    SqlCommand com = new SqlCommand(reset, conn);
    com.Parameters.AddWithValue("@pass", resetpassword);
    com.Parameters.AddWithValue("@user", TextBox1.Text);
    conn.Close();
}

由于某种原因,密码未更新。

2 个答案:

答案 0 :(得分:1)

您忘记执行查询:

int cnt = com.ExecuteNonQuery();

该方法返回受影响的行数。

答案 1 :(得分:1)

执行查询

protected void Button6_Click(object sender, EventArgs e)
{
    Random rnd = new Random();
    string resetpassword = rnd.Next(5000, 100000).ToString();
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HealthDBContext"].ConnectionString);
    conn.Open();
    string reset = "UPDATE Users SET" + " Password=@pass" + " WHERE UserName=@user";
    SqlCommand com = new SqlCommand(reset, conn);
    com.Parameters.AddWithValue("@pass", resetpassword);
    com.Parameters.AddWithValue("@user", TextBox1.Text);
    com.ExecuteNonQuery();
    conn.Close();
}