// 刷新用户信息列表 private void button_UserManage_Refresh_Click(object sender, EventArgs e) { button_UserManage_Delete.Enabled = false; try { // SQL string sql = "select user_id, user_name, user_type, reg_date, user_email from user_info"; // DataRead Process MySqlConnection conn = new MySqlConnection(MySqlHelper.getConn()); conn.Open(); MySqlCommand cmd = conn.CreateCommand(); cmd.CommandText = sql; MySqlDataReader sdr = cmd.ExecuteReader(); listView_UserInfo.Items.Clear(); while (sdr.Read()) { string userID = sdr.GetString(sdr.GetOrdinal("user_id")); string userName = sdr.GetString(sdr.GetOrdinal("user_name")); string userType_str = sdr.GetString(sdr.GetOrdinal("user_type")); string regDate = sdr.GetString(sdr.GetOrdinal("reg_date")); string userEmail = sdr.GetString(sdr.GetOrdinal("user_email")); string userType = ""; if ("admin" == userType_str) { userType = "系统管理员"; } else if ("user" == userType_str) { userType = "用户"; } if (userName == CurrentUserInfo.Name) { userName += "(自己)"; } ListViewItem item = new ListViewItem(userID); item.SubItems.Add(userName); item.SubItems.Add(userType); item.SubItems.Add(regDate); item.SubItems.Add(userEmail); listView_UserInfo.Items.Add(item); } conn.Close(); } catch { MessageBox.Show("Error !"); } }
// 提交按钮 private void button_Register_Click(object sender, EventArgs e) { if (textBox_Code.Text != forgotInfo.Code) { MessageBox.Show("验证码不正确!"); } else if (textBox_UserPass.Text == "") { MessageBox.Show("请输入密码"); } else if (textBox_UserPassCheck.Text == "") { MessageBox.Show("请确认密码"); } else if (textBox_UserPass.Text != textBox_UserPassCheck.Text) { MessageBox.Show("密码不一致!"); } else { try { // SQL string sql = "update user_info set user_pass = '******' where user_name = '" + textBox_UserName.Text + "'"; // DataRead Process MySqlConnection conn = new MySqlConnection(MySqlHelper.getConn()); conn.Open(); MySqlCommand cmd = new MySqlCommand(sql, conn); int i = 0; i = cmd.ExecuteNonQuery(); if (i > 0) { // Log LogHelper.generateLog("[忘记密码] 用户 " + textBox_UserName.Text + " 重置密码成功"); MessageBox.Show("密码重置成功!"); this.Close(); } else { MessageBox.Show("更新失败!"); } conn.Close(); } catch { MessageBox.Show("Error !"); } } }
// 删除一条用户信息 private void button_UserManage_Delete_Click(object sender, EventArgs e) { string UserName = listView_UserInfo.SelectedItems[0].SubItems[1].Text; try { // SQL string sql = "delete from user_info where user_name = '" + UserName + "'"; // DataRead Process MySqlConnection conn = new MySqlConnection(MySqlHelper.getConn()); conn.Open(); MySqlCommand cmd = new MySqlCommand(sql, conn); if (MessageBox.Show("确定要删除?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes) { int i = 0; i = cmd.ExecuteNonQuery(); if (i > 0) { MessageBox.Show("用户 " + UserName + " 记录已删除!"); } else { MessageBox.Show("删除失败!"); } conn.Close(); button_UserManage_Refresh_Click(new object(), new EventArgs()); button_UserManage_Delete.Enabled = false; } } catch { MessageBox.Show("Error !"); } }
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 !"); } } }
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("对不起,你输入的账号或者密码错误!"); } }
// 发送按钮 (先查询数据库有无此人,在查询有无邮箱,若有则发送验证码) private void button_Send_Click(object sender, EventArgs e) { // 生成6位验证码 Random rd = new Random(); int num = rd.Next(100000, 1000000); forgotInfo.Code = num.ToString(); if (textBox_UserName.Text == "") { MessageBox.Show("用户名不能为空"); } else { //try { // SQL string sql = "select user_email from user_info where user_name = '" + textBox_UserName.Text + "'"; // DataRead Process MySqlConnection conn = new MySqlConnection(MySqlHelper.getConn()); conn.Open(); MySqlCommand cmd = conn.CreateCommand(); cmd.CommandText = sql; MySqlDataReader sdr = cmd.ExecuteReader(); // 有该用户,且该用户填写邮箱 if (sdr.Read() && "" != sdr.GetString(sdr.GetOrdinal("user_email"))) { // 展开窗口 for (int i = 190; i <= 320; i += 2) { Size = new Size(310, i); } label_UserPass.Visible = true; label_UserPassCheck.Visible = true; textBox_UserPass.Visible = true; textBox_UserPassCheck.Visible = true; button_cancel.Visible = true; button_Register.Visible = true; textBox_UserName.Enabled = false; label_Note.Visible = false; // 读取邮件 forgotInfo.Email = sdr.GetString(sdr.GetOrdinal("user_email")); // 发送包含验证码的邮件 send_email(); // Log LogHelper.generateLog("[忘记密码] 向用户 " + textBox_UserName.Text + " 发送了一封验证邮件"); // 在新线程中处理60s计数操作 Thread thread_code_process = new Thread(new ThreadStart(code_process)); thread_code_process.Start(); MessageBox.Show("验证码已发送到您的邮箱:" + forgotInfo.Email + "中!"); } else { MessageBox.Show("无此用户或该用户未填写邮箱信息!"); } conn.Close(); } //catch { // MessageBox.Show("Error !"); } } }