if (this.tabControl1.SelectedTab.Text == this.tabPage2.Text)
{
string user2 = this.textBox4.Text.ToString().Trim();
string psw21 = this.textBox5.Text.ToString().Trim();
string psw22 = this.textBox6.Text.ToString().Trim();
string psw23 = this.textBox7.Text.ToString().Trim();
if (psw22 == psw23)//PSW22和PSW23是输入的新密码 和 确认密码
{
try
{
con.Open();
string sql = "select * from admin"; //连接SQL数据库
SqlCommand com = new SqlCommand(sql, con);
SqlDataReader sdr = com.ExecuteReader();
sdr.Read();
if (Convert.ToString(sdr["password"]) == psw21 && Convert.ToString(sdr["user"]) == user2) //核对用户名和原来的老密码
{
string sql1 = "update admin set user='" + user2 + "',password='" + psw22 + "'";
SqlCommand com1 = new SqlCommand(sql1, con);
com.ExecuteNonQuery();//错误提示“已有打开的与此命令相关联的 DataReader,必须首先将它关闭。”大家帮我看看应该怎么修改
con.Close();
MessageBox.Show("用户密码修改成功!", "完成");
this.textBox4.Text = "";
this.textBox5.Text = "";
this.textBox6.Text = "";
this.textBox7.Text = "";
}
else if (Convert.ToString(sdr["user"]) != user2)
{
MessageBox.Show("用户不存在,请重新输入!", "失败");
this.textBox4.Text = "";
this.textBox5.Text = "";
this.textBox6.Text = "";
this.textBox7.Text = "";
}else if(Convert.ToString(sdr["password"]) != psw21)
{
MessageBox.Show("旧密码错误,请重新输入!", "错误");
this.textBox5.Text = "";
} }
catch (SqlException e1)
{
MessageBox.Show("用户不存在,请重新输入!", "失败");
this.textBox4.Text = "";
this.textBox5.Text = "";
this.textBox6.Text = "";
this.textBox7.Text = "";
}
finally
{
con.Close();
}
}
else
{
MessageBox.Show("两次输入密码不相同,请重新输入!", "错误");
this.textBox6.Text = "";
this.textBox7.Text = "";
}
}这是修改登陆密码的点击确定的源代码
{
string user2 = this.textBox4.Text.ToString().Trim();
string psw21 = this.textBox5.Text.ToString().Trim();
string psw22 = this.textBox6.Text.ToString().Trim();
string psw23 = this.textBox7.Text.ToString().Trim();
if (psw22 == psw23)//PSW22和PSW23是输入的新密码 和 确认密码
{
try
{
con.Open();
string sql = "select * from admin"; //连接SQL数据库
SqlCommand com = new SqlCommand(sql, con);
SqlDataReader sdr = com.ExecuteReader();
sdr.Read();
if (Convert.ToString(sdr["password"]) == psw21 && Convert.ToString(sdr["user"]) == user2) //核对用户名和原来的老密码
{
sdr.close();
string sql1 = "update admin set user='" + user2 + "',password='" + psw22 + "'";
SqlCommand com1 = new SqlCommand(sql1, con); com.ExecuteNonQuery();//错误提示“已有打开的与此命令相关联的 DataReader,必须首先将它关闭。”大家帮我看看应该怎么修改
con.Close();
MessageBox.Show("用户密码修改成功!", "完成");
this.textBox4.Text = "";
this.textBox5.Text = "";
this.textBox6.Text = "";
this.textBox7.Text = "";
}
else if (Convert.ToString(sdr["user"]) != user2)
{
MessageBox.Show("用户不存在,请重新输入!", "失败");
this.textBox4.Text = "";
this.textBox5.Text = "";
this.textBox6.Text = "";
this.textBox7.Text = "";
}
else if (Convert.ToString(sdr["password"]) != psw21)
{
MessageBox.Show("旧密码错误,请重新输入!", "错误");
this.textBox5.Text = "";
} }
catch (SqlException e1)
{
MessageBox.Show("用户不存在,请重新输入!", "失败");
this.textBox4.Text = "";
this.textBox5.Text = "";
this.textBox6.Text = "";
this.textBox7.Text = "";
}
finally
{
con.Close();
}
}
else
{
MessageBox.Show("两次输入密码不相同,请重新输入!", "错误");
this.textBox6.Text = "";
this.textBox7.Text = "";
} }
如果只有一条记录还用数据库干什么?
如果不是,要修改的账户正好在第一条记录吗?
建议第一条SQL语句修改为:select * from admin where user=@user,只获取第一条记录。sdr.Read(); //该为if(sdr.Read()){}或者两条合为一条:
update admin set password=@newPassword where user=@user and password=@oldPassword执行方式改为:
int result=com.ExecuteNonQuery();
if(1==result)
{
//用户密码修改成功
}
else
{
//用户名或密码错误,信息更隐蔽
}