2.6 密码修改界面
2018-12-05 本文已影响0人
略略丶略
2.6 密码修改
第一小组
领鼎小组---毛运崟
主要功能
- 用户登录成功后,进入系统主界面。
- 用户通过点击主界面上的“修改密码”菜单,打开密码修改界面。
- 密码修改界面上,默认显示出当前登录的用户名。
2.6修改密码.gif
数据表结构

ADO.NET更新数据库的流程
- 导入命名空间;
- 定义数据库连接字符串,创建Connection对象;
- 打开连接;
- 利用Command对象的ExecuteNonQuery()方法执行Update语句;
- 通过ExecuteNonQuery()方法返回值判断是否修改成功,并在界面上提示;
- 关闭连接。
关键代码
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();
// 构造命令
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("密码修改错误");
}
}
catch (Exception exp)
{
MessageBox.Show("访问数据库错误:" + exp.ToString());
}
finally
{
sqlConn.Close();
}
}
// 在“新密码”输入框中按“回车”,光标跳转到“确认密码”输入框
private void tb_NewPwd_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
SendKeys.Send("{tab}");
}
}
// 在“确认密码”输入框中按“回车”,则直接修改密码
private void tb_ConfirmPwd_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
this.bt_Ok_Click(sender, e);
}
}
}
}