2.5|2.5 登录用户验证功能设计

GIF效果图 2.5|2.5 登录用户验证功能设计
文章图片
效果图.gif 描述画面主要功能,列出支持这些功能的后台数据库表结构 收银员、库管员登陆成功的显示状态,实现登录信息到数据库服务器上验证,显示数据库 SuperMarketSales下的收银员用户表和库管员用户表

dr.Read(); UserInfo.userId = int.Parse(dr["ID"].ToString()); UserInfo.userName = dr["NAME"].ToString(); UserInfo.userPwd = dr["PASSWORD"].ToString(); UserInfo.userPhone = dr["PHONE"].ToString(); UserInfo.userType = this.cbb_UserType.Text;

ADO.NET查询数据库的流程 2.5|2.5 登录用户验证功能设计
文章图片
image.png
2.5|2.5 登录用户验证功能设计
文章图片
image.png 贴入重要代码片段,并进行详细描述 【2.5|2.5 登录用户验证功能设计】// 点击“登录”按钮则登录系统
private void bt_Login_Click(object sender, EventArgs e)
{
String connStr = "Data Source=.; Initial Catalog=SuperMarketSales; Integrated Security=True";
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
sqlConn.Open();
String sqlStr = ""; if (this.cbb_UserType.Text == "收银员") { // 注意USER是SQL Server关键字,表名不能命名为USER,而应当用USERS sqlStr = "select * from USERS where ID=@id and PASSWORD=@pwd"; } else { sqlStr = "select * from ADMIN where ID=@id and PASSWORD=@pwd"; }SqlCommand cmd = new SqlCommand(sqlStr, sqlConn); // 注意是用用户ID登录,而不是用户名,用户名可能会重复 cmd.Parameters.Add(new SqlParameter("@id", this.tb_User.Text.Trim())); cmd.Parameters.Add(new SqlParameter("@pwd", this.tb_Password.Text.Trim())); SqlDataReader dr = cmd.ExecuteReader(); // 如果从数据库中查询到记录,则表示可以登录 if (dr.HasRows) { dr.Read(); UserInfo.userId = int.Parse(dr["ID"].ToString()); UserInfo.userName = dr["NAME"].ToString(); UserInfo.userPwd = dr["PASSWORD"].ToString(); UserInfo.userPhone = dr["PHONE"].ToString(); UserInfo.userType = this.cbb_UserType.Text; MessageBox.Show(UserInfo.userType + "登录成功"); if (UserInfo.userType == "收银员") { // 显示收银员主界面 MainFormUser formUser = new MainFormUser(); formUser.Show(); // 隐藏登录界面 this.Hide(); }if (UserInfo.userType == "库管员") { // 显示库管员主界面 MainFormAdmin formAdmin = new MainFormAdmin(); formAdmin.Show(); // 隐藏登录界面 this.Hide(); } } else { MessageBox.Show("用户名或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception exp) { MessageBox.Show("数据库连接失败"); } finally { sqlConn.Close(); } }

    推荐阅读