美文网首页
登陆界面与录入

登陆界面与录入

作者: 刘利洋 | 来源:发表于2018-07-09 16:57 被阅读0次

1.界面

1.1登陆界面
图1.PNG
1.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();

相关文章

  • 登陆界面与录入

    1.界面 1.1登陆界面 1.2录入界面 2.代码 2.1登陆查询 2.2录入信息 2.3读取数据库信息

  • 2.7 商品信息录入界面功能设计

    一.商品录入功能界面 二.商品录入成功时 三.录入界面效果图 四. 登陆界面各控件的参数设置 控件A:Lable ...

  • 2018-07-09

    考勤系统的登录与录入 登录界面 1)登录界面代码 录入界面 1)录入界面代码 注册界面 1)注册信息代码 1)数据...

  • 2019-07-03 3.考勤系统界面设计

    一、考勤系统登陆界面 二、员工考勤主界面 三、管理员主界面 四、密码修改界面 五、新员工录入界面 六、考勤系统查询...

  • 初入Github

    初入Github 在Github首页申请一个账号,然后登陆。 1. Github主界面与设置界面 登陆后的初始界面...

  • 商品信息录入界面功能设计

    1.设计并设计商品信息录入界面 2.录入界面功能描述 该商品录入界面实现功能:①登录窗口出现在屏幕正中央,并且不能...

  • 2018-12-03

    2.7商品信息录入界面功能设计 1.商品信息录入界面效果图 2.商品信息录入界面主要功能 在智慧社区商超管理系统中...

  • 2018-12-02

    2.7 商品信息录入界面功能设计 一、效果图 录入商品信息 二、功能描述 利用库管员身份登录到管理界面,单击 录入...

  • 2018-04-04(1)

    关于智慧商超管理系统的登陆界面详述文档 一、登陆界面的效果图 二、登陆界面实现的功能描述 对用户类型(收银员与库管...

  • 2018-03-16

    关于智慧商超管理系统的登陆界面详述文档 一、登陆界面的效果图 二、登陆界面实现的功能描述 对用户类型(收银员与库管...

网友评论

      本文标题:登陆界面与录入

      本文链接:https://www.haomeiwen.com/subject/hkstpftx.html