// 判断注册用户是否已经存在 bool UserName(string userName) { string sqlStr = "select userName from user_info where userName='******'"; DataSet ds = DBconn.getData(sqlStr); if (ds.Tables[0].Rows.Count == 0) { return(false); } return(true); }
// 加载数据到列表方法 public void AddData(string sql) { try { // 首先清空页面 while (this.dataGridView1.Rows.Count != 0) { this.dataGridView1.Rows.RemoveAt(0); } DataSet ds = DBconn.getData(sql); int count = ds.Tables[0].Rows.Count; // 逐行加载数据到列表 for (int i = 0; i < count; i++) { string id = ds.Tables[0].Rows[i][0].ToString().Trim(); string userid = ds.Tables[0].Rows[i][1].ToString().Trim(); string content = ds.Tables[0].Rows[i][2].ToString().Trim(); string dateTime = ds.Tables[0].Rows[i][3].ToString().Trim(); string status = ds.Tables[0].Rows[i][4].ToString().Trim(); string address = ds.Tables[0].Rows[i][5].ToString().Trim(); string type = ds.Tables[0].Rows[i][6].ToString().Trim(); // 获取用户名 string sqlName = "select userName from user_info where userId = '" + int.Parse(userid) + "'"; DataSet dataSet = DBconn.getData(sqlName); // 加载数据到列表 this.dataGridView1.Rows.Add(id, dataSet.Tables[0].Rows[0][0], content, dateTime, address, type, status); // 设置按键背景色 if (status.Equals("待维修")) { dataGridView1.Rows[i].Cells[6].Style.BackColor = Color.Pink; } else { dataGridView1.Rows[i].Cells[6].Style.BackColor = Color.Aqua; } } } catch (Exception ex) { MessageBox.Show("获取数据失败" + ex.Message); } finally { // 关闭数据库 DBconn.conn.Close(); } }
private void SuperAdminForm_Load(object sender, EventArgs e) { // 右下角时间控制 this.toolStripStatusLabel1.Alignment = ToolStripItemAlignment.Right; this.toolStripStatusLabel1.Text = "当前日期为:" + DateTime.Now.ToShortDateString(); // 设置下拉列表 this.RolesColumn.Items.Add("后勤老师"); this.RolesColumn.Items.Add("维修工程师"); this.RolesColumn.Items.Add("学生"); // 设置id无法修改 userGridView.Columns[0].ReadOnly = true; // 从数据库中取数据,加载到列表控件上 try { // SQL语句 string sql = "select * from user_info"; // 1.连接数据库,并执行SQL语句 DataSet dataSet = DBconn.getData(sql); // 2.通过for循环加载数据 for (int i = 0; i < dataSet.Tables[0].Rows.Count; i++) { // 定义字符串接收对应数据 string userId = dataSet.Tables[0].Rows[i][0].ToString(); string userName = dataSet.Tables[0].Rows[i][1].ToString(); string userPwd = dataSet.Tables[0].Rows[i][2].ToString(); string userRoles = dataSet.Tables[0].Rows[i][3].ToString(); this.userGridView.Rows.Add(userId, userName, userPwd); //MessageBox.Show( this.RolesColumn.Items[i].ToString()); this.userGridView.Rows[i].Cells[3].Style.NullValue = userRoles; // this.userGridView.DataSource = dataSet.Tables[0]; } } catch (Exception ex) { MessageBox.Show("加载出错:" + ex.Message); } finally { // 关闭数据库连接 DBconn.conn.Close(); } }
/// <summary> /// 登录按钮事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonLogin_Click(object sender, EventArgs e) { try { // 1. 验证输入数据是否合法 // 获取文本框的值 string name = this.userName.Text.Trim(); string pwd = this.passWord.Text.Trim(); string role = this.Roles.Text.Trim(); // 验证输入的值 if (name.Equals("")) { MessageBox.Show("用户名不能为空!", "提示"); // 获取焦点 userName.Focus(); return; } else if (pwd.Equals("")) { MessageBox.Show("密码不能为空", "提示"); // 获取焦点 passWord.Focus(); return; } else if (Roles.Equals("")) { MessageBox.Show("身份不能为空", "提示"); // 获取焦点 Roles.Focus(); return; } // 数据库查询用户信息 string sqlStr = "select userName, userPwd, roles from user_info where userName = '******'"; DataSet ds = DBconn.getData(sqlStr); // 一个大坑,数据库取出来的数据的字符串中有空字符,需要使用trim()去掉 // string roles = ds.Tables[0].Rows[0][2].ToString().Trim(); int count = ds.Tables[0].Rows.Count; if (count == 0) { MessageBox.Show("用户名不存在,请重新输入", "提示"); // 文本框置空 userName.Text = ""; passWord.Text = ""; Roles.Text = ""; userName.Focus(); } else if (ds.Tables[0].Rows[0][1].ToString().Trim() == pwd) { string roles = ds.Tables[0].Rows[0][2].ToString().Trim(); // 验证身份 if (roles.Equals("后勤老师") && roles.Equals(role)) { MessageBox.Show("恭喜您登录成功!", "提示"); // 验证通过,页面跳转 SuperAdminForm superAdminForm = new SuperAdminForm(); this.Hide(); superAdminForm.Show(); } else if (roles.Equals("维修工程师") && roles.Equals(role)) { MessageBox.Show("恭喜您登录成功!", "提示"); // 验证通过,页面跳转 AdminForm adminForm = new AdminForm(); this.Hide(); adminForm.Show(); } else if (roles.Equals("学生") && roles.Equals(role)) { // 验证通过,页面跳转 MessageBox.Show("恭喜您登录成功!", "提示"); MainForm mainForm = new MainForm(); mainForm.LabUser.Text = name; this.Hide(); mainForm.Show(); } else { MessageBox.Show("请正确选择您的身份!"); Roles.Text = ""; userName.Focus(); } } else { MessageBox.Show("密码错误!请重新输入!"); userName.Text = ""; passWord.Text = ""; Roles.Text = ""; userName.Focus(); } } catch (Exception ex) { MessageBox.Show("登录异常:" + ex.Message); userName.Text = ""; passWord.Text = ""; Roles.Text = ""; userName.Focus(); } finally { // 最后关闭数据库 DBconn.conn.Close(); } }
private void ButRepair_Click(object sender, EventArgs e) { try { // 1.验证输入内容是否有效 if (TextAddress.Text.Trim().Equals("")) { MessageBox.Show("报修地点不能为空", "提示"); TextAddress.Focus(); return; } else if (TextContent.Text.Trim().Equals("")) { MessageBox.Show("报修内容不能为空", "提示"); TextContent.Focus(); return; } else if (ComType.SelectedIndex == -1) { MessageBox.Show("请选择报修类型", "提示"); ComType.Focus(); return; } // 验证数据库中是否已经有相同的报修 string id = "select userId from user_info where userName = '******'"; DataSet user = DBconn.getData(id); string sqlStr = "select repairId,repairCount from repairtable where userId = '" + user.Tables[0].Rows[0][0] + "'"; DataSet ds = DBconn.getData(sqlStr); //如果ds里面不存在数据,说明这个用户第一次报修,直接插入新的记录 if (ds.Tables[0].Rows.Count == 0) { string sqlInsert = "insert into repairtable(userId,repairContent,repairDate," + "repairStatus,repairAddress,repairType,repairCount)values('" + user.Tables[0].Rows[0][0] + "','" + TextContent.Text + "','" + DateRepair.Value + "','" + "待维修" + "','" + TextAddress.Text + "','" + ComType.Text + "','" + 1 + "')"; DBconn.upData(sqlInsert); MessageBox.Show("报修成功!"); ClearAll(); } else { //否则的话就是repairtable表里已经有了LanUser里的这个用户。目的就是更新这个用户的报修次数,并插入新的记录 int count = ds.Tables[0].Rows.Count; string sqlInsert = "insert into repairtable(userId,repairContent,repairDate," + "repairStatus,repairAddress,repairType,repairCount)values('" + user.Tables[0].Rows[0][0] + "','" + TextContent.Text + "','" + DateRepair.Value + "','" + "待维修" + "','" + TextAddress.Text + "','" + ComType.Text + "','" + (count + 1) + "')"; DBconn.upData(sqlInsert); MessageBox.Show("报修成功!"); ClearAll(); } } catch (Exception ex) { MessageBox.Show("报修出错:" + ex.Message); TextContent.Focus(); } finally { // 最后关闭数据库 DBconn.conn.Close(); } }