《2.6 密码修改界面功能设计》:
![](https://img.haomeiwen.com/i14032863/0337112eb147a2af.gif)
通过输入原用户账户与密码,经过数据库确认,修改原有密码并录入数据库中。
ADO .NET实现修改数据库步骤
具体步骤:
- 导入命名空间;
- 定义数据库连接字符串,创建Connection对象;
- 打开连接;
- 利用Command对象的ExecuteNonQuery()方法执行Update语句;
- 通过ExecuteNonQuery()方法返回值判断是否修改成功,并在界面上提示;
- 关闭连接。
重要代码(1) 验证两次密码是否重合
if (newPwd.Equals(""))
{
MessageBox.Show("请输入新密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else if (confPwd.Equals(""))
{
MessageBox.Show("请输入确认密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else if (newPwd != confPwd)
{
MessageBox.Show("两次密码不一致", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
重要代码(2)
// 构造UPDATE命令
String sqlStr = "update EMPLOYEE set PASSWORD=@pwd where ID=@id";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);(发送更新数据代码给数据库)
// SQL字符串参数赋值
cmd.Parameters.Add(new SqlParameter("@pwd", newPwd));
cmd.Parameters.Add(new SqlParameter("@id", UserInfo.userId));(新数据值为输入值)
// 将命令发送给数据库
int res = cmd.ExecuteNonQuery();
// 根据返回值判断是否修改成功
if (res != 0)
{
MessageBox.Show("密码修改成功");
this.Close();
}
else
{
MessageBox.Show("密码修改错误");
}
网友评论