2.6 密码修改界面功能设计
效果浏览图:
修改密码.gif主要功能:
用户登录成功后进入系统主界面,用户点击主界面上的修改密码菜单,打开修改密码界面,然后进行密码修改。
后台数据库结构表:
数据库结构表.PNGAD.NET更新数据库流程:
(1) 导入命名空间;
(2) 定义数据库连接字符串,创建Connection对象;
(3) 打开连接;
(4) 利用Command对象的ExecuteNonQuery()方法执行Insert语句;
(5) 通过ExecuteNonQuery()方法返回值判断是否修改成功,并在界面上提示;
(6) 关闭连接。
重要代码:
String userName = this.tb_User.Text.Trim();
String newPwd = this.tb_NewPwd.Text.Trim();
String confPwd = this.tb_ConfirmPwd.Text.Trim();
// 验证输入信息
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;
}
修改密码
// 连接字符串,注意与实际环境保持一致
String connStr = "Data Source=.;Initial Catalog=SuperMarketSales;Integrated Security=True";
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 连接数据库
sqlConn.Open();
// 构造UPDATE命令,更改数据库,参见后面PPT
}
catch (Exception exp)
{
MessageBox.Show("访问数据库错误:" + exp.Message);
}
finally
{
sqlConn.Close();
}
连接字符串
// 构造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("密码修改错误");
}
判断条件
网友评论