Exemple #1
0
        // 登录按钮事件处理
        private void btnLogin_Click(object sender, EventArgs e)
        {
            bool error = false;   // 标志在执行数据库操作的过程中是否出错

            // 如果输入验证成功,就验证身份,并转到相应的窗体
            if (ValidateInput())
            {
                int num = 0;  // 数据库操作结果

                try
                {
                    // 查询用的sql语句
                    string sql = string.Format("SELECT COUNT(*) FROM Users WHERE Id={0} AND LoginPwd = '{1}'",
                        int.Parse(txtLoginId.Text.Trim()), txtLoginPwd.Text.Trim());
                    // 创建Command 对象
                    SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                    DBHelper.connection.Open();  // 打开数据库连接
                    num = Convert.ToInt32(command.ExecuteScalar());
                }
                catch (Exception ex)
                {
                    error = true;
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    DBHelper.connection.Close();  // 关闭数据库连接
                }

                if (!error && (num == 1))  // 验证通过
                {
                    // 设置登录的用户号码
                    UserHelper.loginId = int.Parse(txtLoginId.Text.Trim());
                    // 创建主窗体
                    MainForm mainForm = new MainForm();
                    mainForm.Show();  // 显示窗体
                    this.Visible = false;  // 当前窗体不可见
                }
                else
                {
                    MessageBox.Show("输入的用户名或密码有误!", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
Exemple #2
0
        // 登录按钮事件处理
        private void btnLogin_Click(object sender, EventArgs e)
        {
            bool error = false;   // 标志在执行数据库操作的过程中是否出错
            // 如果输入验证成功,就验证身份,并转到相应的窗体
            if (ValidateInput())
            {
                int num = 0;  // 数据库操作结果
                try
                {
                    // 查询用的sql语句
                    int loginId = int.Parse(txtLoginId.Text.Trim());
                    String password = txtLoginPwd.Text.Trim();
                    num = DBHelper.GetEntities().Users.Count<User>(item => item.Id == loginId && item.LoginPwd == password);
                }
                catch (Exception ex)
                {
                    error = true;
                    Console.WriteLine(ex.Message);
                }

                if (!error && (num == 1))  // 验证通过
                {
                    // 设置登录的用户号码
                    UserHelper.loginId = int.Parse(txtLoginId.Text.Trim());
                    UserInfo userinfo = DBHelper.GetEntities().UserInfoes.First<UserInfo>(item => item.userId == UserHelper.loginId);
                    userinfo.isLogin = "******";
                    DBHelper.GetEntities().SaveChanges();
                    // 创建主窗体
                    MainForm mainForm = new MainForm();
                    mainForm.Show();  // 显示窗体
                    this.Visible = false;  // 当前窗体不可见
                }
                else
                {
                    MessageBox.Show("输入的用户名或密码有误!", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }