任务2.5 登录用户验证功能设计

1. 界面的效果图

连接数据库
任务2.5 登录用户验证功能设计_第1张图片
1.gif
收银员登录
任务2.5 登录用户验证功能设计_第2张图片
2.gif
库管员登录
3.gif

2. 界面实现的功能描述

打开后自动访问数据库,并列出表中的数据
基于连接数据库后能够用库里的账号密码登录到收银员和库管员的管理界面

3. 界面各控件的参数设置

TextBox控件
属性
name tb_Users
lines String[] Array
Multiline True

TextBox控件

属性
name tb_User
lines String[] Array

TextBox控件

属性
name tb_Password
lines String[] Array

4. 想一想,还有哪些尚需完善的功能

实现注册新用户和忘记密码功能

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();

                // 注意USER是SQL Server关键字,表名不能命名为USER,而应当用USERS
                String sqlStr = "select * from EMPLOYEE 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.userType = dr["TYPE"].ToString();
                    UserInfo.userPhone = dr["PHONE"].ToString();

                    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("数据库连接失败");
                return;
            }
            finally
            {
                sqlConn.Close();
            }
        }

        // 点击“退出”按钮则退出应用程序
        private void bt_Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        // 在用户名输入框中按“回车”,光标跳转到密码输入框
        private void tb_User_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                SendKeys.Send("{tab}");
            }
        }

        // 在密码输入框中按“回车”,则直接登录
        private void tb_Password_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                this.bt_Login_Click(sender, e);
            }
        }

        // Tab进入用户名输入框时,自动全选用户名
        private void tb_User_Enter(object sender, EventArgs e)
        {
            ((TextBox)sender).SelectAll();
        }

        // Tab进入密码输入框时,自动全选密码
        private void tb_Password_Enter(object sender, EventArgs e)
        {
            ((TextBox)sender).SelectAll();
        }
    }

你可能感兴趣的:(任务2.5 登录用户验证功能设计)