登陆界面与录入

1.界面

1.1登陆界面
登陆界面与录入_第1张图片
图1.PNG
1.2录入界面
登陆界面与录入_第2张图片
图2.PNG

2.代码

2.1登陆查询
            String connStr = ConfigurationManager.ConnectionStrings["Attendance"].ConnectionString;
            SqlConnection sqlConn = new SqlConnection(connStr);
            try
            {
                // 连接数据库
                sqlConn.Open();

                // 构造命令发送给数据库
                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.userRole = dr["ROLE"].ToString();
                    UserInfo.userDepartment = dr["DEPARTMENT"].ToString();
                    UserInfo.userGender = dr["GENDER"].ToString();

                    MessageBox.Show(UserInfo.userRole + "登录成功");

                    if (UserInfo.userRole == "收银员")
                    {
                        // 显示收银员主界面
                        //MainFormUser formUser = new MainFormUser();
                        //formUser.Show();

                        //// 隐藏登录界面
                        //this.Hide();
                    }

                    if (UserInfo.userRole == "库管员")
                    {
                        //// 显示库管员主界面
                        //MainFormAdmin formAdmin = new MainFormAdmin();
                        //formAdmin.Show();

                        //// 隐藏登录界面
                        //this.Hide();
                    }
                }
                else
                {
                    MessageBox.Show("用户名或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show("访问数据库错误:" + exp.Message);
            }
            finally
            {
                sqlConn.Close();
            }
2.2录入信息
            String ID = this.tb_id.Text.Trim();
            String NAME = this.tb_name.Text.Trim();
            String GENDER = this.tb_gender.Text.Trim();
            String DEPARTMENT = this.tb_department.Text.Trim();
            String ROLE = this.tb_role.Text.Trim();
            String PASSWORD = this.tb_password.Text.Trim();

            // 连接字符串,注意与实际环境保持一致
            String connStr = ConfigurationManager.ConnectionStrings["Attendance"].ConnectionString;
            SqlConnection sqlConn = new SqlConnection(connStr);
            try
            {
                // 连接数据库
                sqlConn.Open();

                // 构造命令
                String sqlStr = "insert into employee(id, name,gender, department, role, password ) values(@ID, @NAME, @GENDER, @DEPARTMENT, @ROLE, @PASSWORD)";
                SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);

                // SQL字符串参数赋值
                cmd.Parameters.Add(new SqlParameter("@ID", ID));
                cmd.Parameters.Add(new SqlParameter("@NAME", NAME));
                cmd.Parameters.Add(new SqlParameter("@GENDER", GENDER));
                cmd.Parameters.Add(new SqlParameter("@DEPARTMENT", DEPARTMENT));
                cmd.Parameters.Add(new SqlParameter("@ROLE", ROLE));
                cmd.Parameters.Add(new SqlParameter("@PASSWORD", PASSWORD));

                // 将命令发送给数据库
                int res = cmd.ExecuteNonQuery();

                // 根据返回值判断是否插入成功
                if (res != 0)
                {
                    MessageBox.Show("信息注册成功");
                }
                else
                {
                    MessageBox.Show("信息注册失败");
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show("访问数据库错误:" + exp.Message);
            }
            finally
            {
                sqlConn.Close();
            }
2.3读取数据库信息
            tb_gender.SelectedIndex = 0;
            tb_role.SelectedIndex = 0;
            String connStr = ConfigurationManager.ConnectionStrings["Attendance"].ConnectionString;
            SqlConnection sqlConn = new SqlConnection(connStr);
            sqlConn.Open();
            String sqlStr0 = "select MAX(id+1) as id from employee";
            SqlCommand cmd = new SqlCommand(sqlStr0, sqlConn);
            SqlDataReader co = cmd.ExecuteReader();
            if (co.HasRows)
            {
                co.Read();
                tb_id.Text = co["ID"].ToString();
            }
            sqlConn.Close();
            sqlConn.Open();
            String sqlStr1 = "select distinct (department) from employee";
            SqlCommand com = new SqlCommand(sqlStr1, sqlConn);
            SqlDataReader xo = com.ExecuteReader();
            if (xo.HasRows)
            {
                while (xo.Read())
                tb_department.Items.Add(xo["department"].ToString());
            }
            tb_department.SelectedIndex = 0;
            sqlConn.Close();

你可能感兴趣的:(登陆界面与录入)