修改密码
- 相关的输入限制
- 判断原密码是否正确
- 将新密码添加到数据库
代码实现
U层
public partial class Login : Form
{
public static string id;//在登录窗体中定义一个静态全局变量,登录窗体存在时,变量id的值就会存在
Boolean flag = false;
flag = user.SelectUser(userinfo.UserId, userinfo.Pwd);
if (flag!=false)
{
id = txtUserName.Text.Trim();//在用户名密码判断完成之后,将用户名文本框中的内容赋给变量id
this.Hide();
this.DialogResult = System.Windows.Forms.DialogResult.OK;
...
}
else
{
MessageBox.Show("密码或用户名错误");
}
}
public partial class ModifyPWD : Form
{
private void btOK_Click(object sender, EventArgs e)
{
//前面是常规有关输入问题的判断
...
try
{
Facade.UserFacade fact = new Facade.UserFacade();
string id = Login.id;//不用实例化登录窗体,使用登录窗体中变量id的值赋给修改密码窗体中的变量id
string password = txtOldPWD.Text;
string newpwd = txtNewPWD.Text;
bool flag = fact.UpdatePwd(id, password,newpwd);//利用三者之间的关系来修改密码
if (flag!=false)
{
MessageBox.Show("修改密码成功!", "提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
this.Hide();
}
else
{
MessageBox.Show("原密码不正确!", "提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
catch (Exception)
{
throw;
}
}
}
Facade层
public Boolean UpdatePwd(string userid,string password,string newpwd)
{
bool flag = userBll.UpdatePwd(userid, password,newpwd);
return flag;
}
B层
public bool UpdatePwd(string userid,string password,string newpwd)
{
bool flag = idal.UpdatePwd(userid, password,newpwd);
return flag;
}
D层
public bool UpdatePwd(string userid,string password,string newpwd)
{
SqlParameter[] sqlParams = {new SqlParameter("@userId",userid),
new SqlParameter("@password",password),
new SqlParameter("@newpwd",newpwd)};
string sql = @"update [User_Info] set pwd=@newpwd where userId=@userId and pwd=@password ";
return sqlHelper.ExecuteNonQuery(sql, sqlParams, CommandType.Text) > 0;
}