Exemple #1
0
        private void button_Register_Click(object sender, EventArgs e)
        {
            // 邮箱格式正则表达式
            string email    = textBox_UserEmail.Text;
            string emailStr = @"([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,5})+";

            if (textBox_UserName.Text == "")
            {
                MessageBox.Show("用户名不能为空");
            }
            else if (textBox_UserPass.Text == "")
            {
                MessageBox.Show("请输入密码");
            }
            else if (textBox_UserPassCheck.Text == "")
            {
                MessageBox.Show("请确认密码");
            }
            else if (comboBox_UserType.Text == "")
            {
                MessageBox.Show("请选择新用户类型");
            }
            else if (textBox_UserPass.Text != textBox_UserPassCheck.Text)
            {
                MessageBox.Show("密码有误!");
            }
            else if (email != "" && !(System.Text.RegularExpressions.Regex.IsMatch(email, emailStr)))
            {
                MessageBox.Show("邮箱格式不正确", "提示");
            }
            else if ((CurrentUserInfo.Type != "admin") && (comboBox_UserType.Text == "系统管理员"))
            {
                MessageBox.Show("您是普通用户,无权限注册系统管理员用户", "提示");
            }
            else
            {
                // 用户类型字符串
                string userType = "user";
                if (comboBox_UserType.Text == "系统管理员")
                {
                    userType = "admin";
                }

                try
                {
                    // SQL
                    string sql       = "insert into user_info (user_name, user_pass, user_type, reg_date, user_email) values('" + textBox_UserName.Text + "','" + textBox_UserPass.Text + "','" + userType + "','" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "','" + textBox_UserEmail.Text + "')";
                    string sql_check = "select * from user_info where user_name = '" + textBox_UserName.Text + "'";

                    // DataRead Process
                    MySqlConnection conn       = new MySqlConnection(MySqlHelper.getConn());
                    MySqlConnection conn_check = new MySqlConnection(MySqlHelper.getConn());
                    conn.Open();
                    conn_check.Open();
                    MySqlCommand cmd       = new MySqlCommand(sql, conn);
                    MySqlCommand cmd_check = new MySqlCommand(sql_check, conn_check); // 检查该用户名是否存在

                    MySqlDataReader sdr = cmd_check.ExecuteReader();
                    if (!sdr.Read()) // 数据库中无此用户名
                    {
                        int i = 0;
                        i = cmd.ExecuteNonQuery();

                        if (i > 0)
                        {
                            // Log
                            LogHelper.generateLog("[新用户注册] " + comboBox_UserType.Text + " " + textBox_UserName.Text + " 注册成功");

                            MessageBox.Show("用户 " + textBox_UserName.Text + " 注册成功!");
                            this.Hide();
                        }
                        else
                        {
                            MessageBox.Show("添加失败!");
                        }
                    }
                    else
                    {
                        MessageBox.Show("该用户已存在!");
                    }

                    conn.Close();
                }
                catch
                {
                    MessageBox.Show("Error !");
                }
            }
        }
Exemple #2
0
        private void button_Login_Click(object sender, EventArgs e)
        {
            //用户类型
            string user_type = "user";

            if (comboBox_UserType.Text == "用户")
            {
                user_type = "user";
            }
            else if (comboBox_UserType.Text == "系统管理员")
            {
                user_type = "admin";
            }

            // SQL
            string sql = "select * from user_info where user_name = '" + textBox_UserName.Text + "' and user_pass = '******' and user_type = '" + user_type + "'";

            // DataRead Process
            MySqlConnection conn = new MySqlConnection(MySqlHelper.getConn());

            conn.Open();
            MySqlCommand cmd = conn.CreateCommand();

            cmd.CommandText = sql;
            MySqlDataReader sdr = cmd.ExecuteReader();


            if (textBox_UserName.Text == "" || textBox_UserPass.Text == "")
            {
                MessageBox.Show("用户名或密码不能为空!");
            }
            else if (comboBox_UserType.Text == "")
            {
                MessageBox.Show("请选择用户类型");
            }
            else if (sdr.Read())
            {
                CurrentUserInfo.Name = sdr.GetString(sdr.GetOrdinal("user_name")).Replace(" ", "");
                CurrentUserInfo.Type = user_type;
                CurrentUserInfo.Id   = Convert.ToInt32(sdr.GetString(sdr.GetOrdinal("user_id")));
                conn.Close();

                MessageBox.Show("欢迎您," + CurrentUserInfo.Name + "!", "登录成功!");
                Application.OpenForms["Main"].Hide();
                this.Hide();

                // 登录身份
                Main_Admin main_admin = new Main_Admin();
                Main_User  main_user  = new Main_User();

                if (CurrentUserInfo.Type == "admin")
                {
                    main_admin.Show();
                }
                else if (CurrentUserInfo.Type == "user")
                {
                    main_user.Show();
                }

                // Log
                LogHelper.generateLog(CurrentUserInfo.Name + " 登录");
            }
            else
            {
                MessageBox.Show("对不起,你输入的账号或者密码错误!");
            }
        }