private void DeleteProfessor(object sender, EventArgs e) { //删除教授子流程,弹出选框要求输入教授ID号,输入后查询并回显,询问是否删除 InputID tempform = new InputID(); tempform.ShowDialog(); if (tempform.Text.Equals("no")) { return; } MySqlConnection conn = new MySqlConnection(mysqlConnectionString); try { conn.Open(); //连接数据库 string sql = "select * from professor where id = '" + tempform.Text + "';"; //查询该ID对应的教授信息 MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlDataReader reader = cmd.ExecuteReader(); //reader内存储了0或1条记录 if (reader.HasRows) { reader.Read(); DialogResult result = MessageBox.Show("姓名:" + reader[1].ToString() + "\n出生日期:" + reader[2].ToString().Substring(0, 9) + "\n职称:" + reader[4].ToString() + "\n学院:" + reader[5].ToString() + "\n是否确定删除该名教授?" , "删除确认", MessageBoxButtons.OKCancel); reader.Close(); if (result == DialogResult.OK) { sql = "delete from professor where id = '" + tempform.Text + "';";//从数据库中删除该教授信息 cmd = new MySqlCommand(sql, conn); cmd.ExecuteNonQuery(); MessageBox.Show("删除成功!"); Show_pro_info(); } else { return; } } else { MessageBox.Show("未找到对应教授!"); DeleteProfessor(null, null); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } conn.Close(); }
private void UpdateProfessor(object sender, EventArgs e) { //更新教授子流程,查询教授ID号,显示教授信息,删除原本数据,插入新数据 InputID tempform = new InputID(); tempform.ShowDialog(); if (tempform.Text.Equals("no")) { return; } MySqlConnection conn = new MySqlConnection(mysqlConnectionString); try { conn.Open(); //连接数据库 string sql = "select * from professor where id = '" + tempform.Text + "';"; //查询该ID对应的教授信息 MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlDataReader reader = cmd.ExecuteReader(); //此时reader储存了1条语句 if (reader.HasRows) { reader.Read(); int status = -1, dept_name = -1; string ID = reader[0].ToString(); string password = reader[6].ToString(); if (reader[4].ToString().Equals("助教")) { status = 0; } if (reader[4].ToString().Equals("讲师")) { status = 1; } if (reader[4].ToString().Equals("副教授")) { status = 2; } if (reader[4].ToString().Equals("教授")) { status = 3; } if (reader[5].ToString().Equals("计算机科学与技术学院")) { dept_name = 0; } if (reader[5].ToString().Equals("数学学院")) { dept_name = 1; } if (reader[5].ToString().Equals("外语学院")) { dept_name = 2; } if (reader[5].ToString().Equals("马克思学院")) { dept_name = 3; } ProInfo proInfo = new ProInfo(); proInfo.update(reader[1].ToString(), reader[2].ToString(), reader[3].ToString(), status, dept_name); proInfo.ShowDialog(); reader.Close(); if (proInfo.Text.Equals("no")) { return; } string[] pro_info = Regex.Split(proInfo.Text, " ", RegexOptions.IgnoreCase); //通过分隔符空格拆开修改窗口返回的必要信息 sql = String.Format("update professor set name='{0}',date_of_birth='{1}',ssn='{2}',status='{3}',dept_name='{4}'" + " where id={5};", pro_info[0], pro_info[1], pro_info[2], pro_info[3], pro_info[4], tempform.Text); //更新教授信息 cmd = new MySqlCommand(sql, conn); int result = cmd.ExecuteNonQuery(); if (result == 1) { MessageBox.Show("教授信息修改成功!"); Show_pro_info(); } } else { MessageBox.Show("未找到对应教授!"); DeleteProfessor(null, null); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } conn.Close(); }
private void UpdateStudent(object sender, EventArgs e) { //更新学生子流程,查询学生ID号,显示学生信息,删除原本数据,插入新数据 InputID tempform = new InputID(); tempform.ShowDialog(); if (tempform.Text.Equals("no")) { return; } MySqlConnection conn = new MySqlConnection(mysqlConnectionString); try { conn.Open(); //连接数据库 string sql = "select * from student where id = '" + tempform.Text + "';"; //查询该ID对应的学生信息 MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlDataReader reader = cmd.ExecuteReader(); //此时reader储存了1条语句 if (reader.HasRows) { reader.Read(); int status = -1, graduate_date = -1; string ID = reader[0].ToString(); string password = reader[6].ToString(); if (reader[4].ToString().Equals("本科生")) { status = 0; } if (reader[4].ToString().Equals("硕士研究生")) { status = 1; } if (reader[4].ToString().Equals("博士研究生")) { status = 2; } if (reader[5].ToString().Equals("2020")) { graduate_date = 0; } if (reader[5].ToString().Equals("2021")) { graduate_date = 1; } if (reader[5].ToString().Equals("2022")) { graduate_date = 2; } if (reader[5].ToString().Equals("2023")) { graduate_date = 3; } if (reader[5].ToString().Equals("2024")) { graduate_date = 4; } StuInfo stuInfo = new StuInfo(); stuInfo.update(reader[1].ToString(), reader[2].ToString(), reader[3].ToString(), status, graduate_date); stuInfo.ShowDialog(); reader.Close(); if (stuInfo.Text.Equals("no")) { return; } string[] pro_info = Regex.Split(stuInfo.Text, " ", RegexOptions.IgnoreCase); //通过分隔符空格拆开修改窗口返回的必要信息 sql = String.Format("update student set name='{0}',date_of_birth='{1}',ssn='{2}',status='{3}',graduate_date='{4}'" + " where id={5};", pro_info[0], pro_info[1], pro_info[2], pro_info[3], pro_info[4], tempform.Text); //更新教授信息 cmd = new MySqlCommand(sql, conn); int result = cmd.ExecuteNonQuery(); if (result == 1) { MessageBox.Show("学生信息修改成功!"); Show_stu_info(); } } else { MessageBox.Show("未找到对应学生!"); DeleteStudent(null, null); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } conn.Close(); }