我有一个登录winform,我把更改密码放在里面。我有此代码用于更新我的数据库中的密码信息。但如果它是真的,它不会读取我的数据读取器,但如果它是假的,它会读取它,并更改我数据库中的密码。
public void ChangePass()
{
sc.Open();
try
{
if (_oldpass == "" || _newpass == "" || _conpass == "")
{
string message = "Must fill up all the fields!";
string title = "Voting System Error Message";
MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
cmd = new SqlCommand("SELECT password FROM TableLogin WHERE password = '" + _oldpass + "'", sc);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read() == true)
{
sc.Close();
if (_newpass == _conpass)
{
sc.Open();
cmd = new SqlCommand("UPDATE TableLogin SET password = '" + _newpass + "' WHERE username = 'admin'", sc);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read() == true)
{
MessageBox.Show("Successfully Changed!");
//This part does not read if true.. but if sdr.Read() == false it changes the password from my database.
}
}
else
{
string message = "New Password and Confirm Password does not match!";
string title = "Voting System Error Message";
MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
string message = "Wrong Old Password!";
string title = "Voting System Error Message";
MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sc.Close();
}
}
我不明白,为什么?