2.7密码修改界面功能设计

2018-05-23  本文已影响0人  Beelalizee

1GIF图片

2.7.gif

2

3

4重要代码及解释

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 = "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("访问数据库错误:" + exp.ToString());
        }
        finally
        {
            sqlConn.Close();
        }

1.连接数据库
2.构造命令

  1. SQL字符串参数赋值
    4.将命令发送给数据库
    5.根据返回值判断是否修改成功
上一篇下一篇

猜你喜欢

热点阅读