2016-07-23 84 views
0
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" + " [email protected]" + " WHERE [email protected]"; 
    SqlCommand com = new SqlCommand(reset, conn); 
    com.Parameters.AddWithValue("@pass", resetpassword); 
    com.Parameters.AddWithValue("@user", TextBox1.Text); 
    conn.Close(); 
} 

由於某些原因,密碼未更新。更新聲明不起作用

+1

OOOOH,請告訴我,你是不是採用明文存儲密碼,在實際應用中。 –

+0

只有當你「插入」,「刪除」,「更新」時,才使用此方法'ExecuteNonQuery();'。等等 –

回答

1

你忘了執行查詢:

int cnt = com.ExecuteNonQuery(); 

該方法返回受影響的行數。

+0

啊!謝謝!總是忘記那條線:( –

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" + " [email protected]" + " WHERE [email protected]"; 
    SqlCommand com = new SqlCommand(reset, conn); 
    com.Parameters.AddWithValue("@pass", resetpassword); 
    com.Parameters.AddWithValue("@user", TextBox1.Text); 
    com.ExecuteNonQuery(); 
    conn.Close(); 
}