Exemplo n.º 1
0
        private void btnLogin_Click(object sender, EventArgs e)
        {
            Cursor.Current           = Cursors.WaitCursor;
            txtBoxErrMessage.Visible = false;

            try
            {
                string userName = txtBoxUserName.Text.Trim();
                if (string.IsNullOrWhiteSpace(userName))
                {
                    txtBoxErrMessage.Visible = true;
                    txtBoxErrMessage.Text    = "用户名不能为空";
                    txtBoxUserName.Focus();
                    Cursor.Current = Cursors.Default;
                    return;
                }

                DataTable dataTable = TableUsersManage.QueryUserName(userName);
                if (dataTable.Rows.Count <= 0)
                {
                    txtBoxErrMessage.Visible = true;
                    txtBoxErrMessage.Text    = "用户不存在";
                    txtBoxUserName.Focus();
                    Cursor.Current = Cursors.Default;
                    return;
                }

                string  password           = txtBoxPassword.Text.Trim();
                bool    isRememberPassword = ckBoxRememberPassword.Checked;
                DataRow dataRow            = dataTable.Rows[0];
                var     item = dataRow.ToExpression <UserInfo>();
                this.User = item(dataRow);
                if (this.User.Password.Equals(password))
                {
                    if (isRememberPassword)
                    {
                        AppConfigHelper.SetConfigValue(userName, password);
                    }

                    string mac   = SystemInfo.GetMac();
                    string lanIp = SystemInfo.GetLanIp();
                    //string wanIp = SystemInfo.GetWanIp();
                    //string wanIp = "";
                    string datetime    = DateTime.Now.ToLocalTime().ToString();
                    string hostName    = SystemInfo.GetHostName();
                    string sysUserName = SystemInfo.GetUserName();

                    this.LoginLogId = TableUsersManage.AddUserLoginLogAndReturnIdentity(userName, datetime, mac, lanIp, hostName, sysUserName, this.User.Id);
                    if (this.LoginLogId >= 0)
                    {
                        //int suc = TableUsersManage.ModifyUserLastLoginLogByUserName(userName, datetime, mac, lanIp, hostName, sysUserName);
                        //if (suc > 0)
                        //{
                        this.UserName     = userName;
                        this.DialogResult = DialogResult.OK;

                        this.Close();
                        //}
                        //else
                        //{
                        //    MessageBox.Show(this, "服务器连接错误!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        //}
                    }
                    else
                    {
                        MessageBox.Show(this, "服务器连接错误!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    txtBoxErrMessage.Visible = true;
                    txtBoxErrMessage.Text    = "密码错误";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            Cursor.Current = Cursors.Default;
        }
Exemplo n.º 2
0
        private void BtnOk_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                string name = TxtBoxUserName.Text.Trim();
                if (name.Contains("'"))
                {
                    MessageBox.Show(this, "含有非法字符 \"'\" !", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    TxtBoxUserName.Focus();
                    return;
                }
                if (WorkingState.Add == this.WorkingState)
                {
                    DataTable dataTable = TableUsersManage.QueryUserName(name);
                    if (dataTable.Rows.Count > 0)
                    {
                        MessageBox.Show(this, "已存在相同的用户名!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        TxtBoxUserName.Focus();
                        return;
                    }
                }

                string password = TxtBoxPassword.Text.Trim();
                if (password.Contains("'"))
                {
                    MessageBox.Show(this, "含有非法字符 \"'\" !", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    TxtBoxPassword.Focus();
                    return;
                }
                if (string.IsNullOrWhiteSpace(password))
                {
                    MessageBox.Show(this, "密码不能为空!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    TxtBoxPassword.Focus();
                    return;
                }

                string nickname = TxtBoxNickname.Text.Trim();
                if (nickname.Contains("'"))
                {
                    MessageBox.Show(this, "含有非法字符 \"'\" !", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    TxtBoxNickname.Focus();
                    return;
                }

                string privilege = ComboBoxPrivilege.Text;
                if (WorkingState.Add == this.WorkingState)
                {
                    int identity = TableUsersManage.AddUserAndReturnIdentity(name, password, privilege, nickname, DateTime.Now.ToLocalTime().ToString());
                    if (identity > 0)
                    {
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show(this, "添加用户失败!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else if (WorkingState.Modify == this.WorkingState)
                {
                    int suc = TableUsersManage.ModifyUserByUserName(name, password, privilege, nickname);
                    if (suc > 0)
                    {
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show(this, "修改用户失败!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            Cursor.Current = Cursors.Default;
        }