PERSONNEL_CHANGE.cs
上传用户:zhongfine
上传日期:2022-08-01
资源大小:2860k
文件大小:12k
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- namespace 人事管理系统
- {
- public partial class PERSONNEL_CHANGE : Form
- {
-
- Datacon mydatacon = new Datacon();
- DataSet mydataset = new DataSet();
- SqlConnection sqlcn = new SqlConnection();
- SqlDataAdapter myadapter;
-
-
-
-
- public PERSONNEL_CHANGE()
- {
- InitializeComponent();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- this.Close();
- major mymajor = new major();
-
- }
- private void button4_Click(object sender, EventArgs e)
- {
- this.Close();
- major mymajor = new major();
-
- }
- private void button3_Click(object sender, EventArgs e)
- {
- if (textBox2.Text == "")
- {
- MessageBox.Show("请输入要辞退的员工号码!");
- return;
- }
- sqlcn = mydatacon.getcon();//同理,调用连接函数
- sqlcn.Open();
-
- String delete = this.textBox2.Text;
- int a = int.Parse(delete);//将字符串型的delete转为int型
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = sqlcn;
- SqlCommand cnd = new SqlCommand("查询员工号是否存在 '" + a.ToString() + "'", sqlcn);
- string q = cnd.ExecuteScalar().ToString();//用q来记录该员工号是否存在
- if (q == "0")//为0表示情况不存在
- {
- MessageBox.Show("你输入的员工号不存在,请重新输入!");
- return;
- }
- else
- {
- cnd.CommandText = "select count(*) from [PERSON] where ID='"+a+"' and JOB='经理'";
- string l=cnd.ExecuteScalar().ToString();
- if (l != "0")
- {
- cmd.CommandText = "select * from [PERSON] where ID='" + a + "'";
- SqlDataReader da = cmd.ExecuteReader();
- da.Read();
- string department = da["DEPARTMENT"].ToString();
- da.Close();
- cmd.CommandText = "update [DEPARTMENT] set MANAGER=null where NAME='"+department+"'";
- cmd.ExecuteNonQuery();
-
- }
- cmd.CommandText = "删除员工 '" + a + "'";
- cmd.ExecuteNonQuery();//若存在,则执行删除命令
-
- cmd.Connection = sqlcn;
- //cmd.CommandText = "更新人事变更记录计数器";//人事变更记录计数器字段加1
- //cmd.ExecuteNonQuery();
- String mysql2 = "查询人事变更记录编号";
- SqlCommand sqlcm = new SqlCommand(mysql2, sqlcn);
- SqlDataReader dr = sqlcm.ExecuteReader();
- dr.Read();
- string num = dr["COUNTER_VAL"].ToString();//读出人事变更记录内的最新值
- dr.Close();
- cmd.CommandText = "记录增加员工 '" + num + "','员工号为" + a.ToString() + "的员工被辞退了!'";//将员工辞退执行完后的记录写进人事变更记录表
- cmd.ExecuteNonQuery();
- sqlcn.Close();
- MessageBox.Show("员工辞退成功!");
- this.textBox2.Clear();
- #region//刷新数据
- DataSet mydataset3 = new DataSet();
- sqlcn = mydatacon.getcon();
- string ccd = "select ID as 员工号, NAME as 姓名, SEX as 性别, BIRTHDAY as 生日, DEPARTMENT as 所在部门, JOB as 职务, EDU_LEVEL as 受教育程度, SPCIALTY as 专业技能, ADDRESS as 家庭住址, TEL as 联系电话, EMAIL as 电子邮箱, STATE as 当前状态, REMARK as 备注 from [PERSON]";
- myadapter = new SqlDataAdapter(ccd, sqlcn);
- mydataset3.Clear();
- myadapter.Fill(mydataset3, "PERSON");
- dataGridView1.DataSource = mydataset3.Tables["PERSON"];
- #endregion
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- if (textBox1.Text == "")
- {
- MessageBox.Show("请输入要变动的员工号码!");
- }
- else
- {
- sqlcn = mydatacon.getcon();
- sqlcn.Open();
- string change = this.textBox1.Text;
- int b = int.Parse(change);
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = sqlcn;
- SqlCommand cnd = new SqlCommand("查询员工号是否存在 '" + b.ToString() + "'", sqlcn);
- string q = cnd.ExecuteScalar().ToString();//同理,用q来记录是否存在该员工号
- if (q == "0")
- {
- MessageBox.Show("你输入的员工号不存在,请重新输入!");
- return;
- }
- else
- {
- if (comboBox2.Text == "经理")
- {
- cmd.CommandText = "select count(*) from [PERSON] where JOB='经理' and DEPARTMENT='" + comboBox1.Text + "'";
- string k = cmd.ExecuteScalar().ToString();
- if (k != "0")
- {
- MessageBox.Show("该部门经理已经存在,请重新输入!");
- return;
- }
- }
- cnd.CommandText = "select count(*) from [PERSON] where ID='" + textBox1.Text + "' and JOB='经理'";
- string l = cnd.ExecuteScalar().ToString();
- if (l != "0")
- {
- cnd.CommandText = "select * from [PERSON] where ID='" + textBox1.Text + "'";
- SqlDataReader da = cnd.ExecuteReader();
- da.Read();
- string department = da["DEPARTMENT"].ToString();
- da.Close();
- cnd.CommandText = "update [DEPARTMENT] set MANAGER=null where NAME='" + department + "'";
- cnd.ExecuteNonQuery();
- }
-
- cmd.CommandText = "update [PERSON] set DEPARTMENT='" + this.comboBox1.Text + "',JOB='" + this.comboBox2.Text + "'where ID='" + b + "'";
- cmd.ExecuteNonQuery();//更新该员工的部门与职位信息
- cmd.CommandText = "select * from [PERSON] where ID='" + textBox1.Text + "'";
- SqlDataReader dn = cmd.ExecuteReader();
- dn.Read();
- string i = dn["NAME"].ToString();
- dn.Close();
- if (comboBox2.Text == "经理")
- {
- cmd.CommandText = "update [DEPARTMENT] set MANAGER='" + i + "' where NAME='" + comboBox1.Text + "'";
- cmd.ExecuteNonQuery();
- }
-
- //cmd.Connection = sqlcn;
- //cmd.CommandText = "更新人事变更记录计数器";//人事变更记录计数器字段加1
- //cmd.ExecuteNonQuery();
- String mysql2 = "查询人事变更记录编号 ";//读出人事变更记录内的最新值
- SqlCommand sqlcm = new SqlCommand(mysql2, sqlcn);
- SqlDataReader dr = sqlcm.ExecuteReader();
- dr.Read();
- string num = dr["COUNTER_VAL"].ToString();//读出计数器内人事变更记录的最新值
- dr.Close();
- cmd.CommandText = "记录增加员工 '" + num + "','员工号为" + b.ToString() + "的员工改变为" + this.comboBox1.Text + "的" + this.comboBox2.Text + "!'";
- cmd.ExecuteNonQuery();//将员工职位改变执行完后的记录写进人事变更记录表
- sqlcn.Close();
- MessageBox.Show("员工职位改变成功!");
- #region//刷新数据
- DataSet mydataset3 = new DataSet();
- sqlcn = mydatacon.getcon();
- string ccd = "select ID as 员工号, NAME as 姓名, SEX as 性别, BIRTHDAY as 生日, DEPARTMENT as 所在部门, JOB as 职务, EDU_LEVEL as 受教育程度, SPCIALTY as 专业技能, ADDRESS as 家庭住址, TEL as 联系电话, EMAIL as 电子邮箱, STATE as 当前状态, REMARK as 备注 from [PERSON]";
- myadapter = new SqlDataAdapter(ccd, sqlcn);
- mydataset3.Clear();
- myadapter.Fill(mydataset3, "PERSON");
- dataGridView1.DataSource = mydataset3.Tables["PERSON"];
- #endregion
- }
- }
- }
- private void PERSONNEL_CHANGE_Load(object sender, EventArgs e)
- {
-
- //sqlcn.Close();
- sqlcn = mydatacon.getcon();
- string cmd = "select ID as 部门编号, NAME as 部门名称 from [DEPARTMENT]";
- myadapter = new SqlDataAdapter(cmd, sqlcn);
- mydataset.Clear();
- myadapter.Fill(mydataset, "DEPARTMENT");
- comboBox1.DataSource = mydataset.Tables["DEPARTMENT"];
- comboBox1.DisplayMember = mydataset.Tables["DEPARTMENT"].Columns[1].ColumnName;
- comboBox1.ValueMember = mydataset.Tables["DEPARTMENT"].Columns[1].ColumnName;
- DataSet mydataset2 = new DataSet();
- string cnd = "select CODE, DESCRIPTION from [JOB] ";
- myadapter = new SqlDataAdapter(cnd, sqlcn);
- mydataset2.Clear();
- myadapter.Fill(mydataset2, "JOB");
- comboBox2.DataSource = mydataset2.Tables["JOB"];
- comboBox2.DisplayMember = mydataset2.Tables["JOB"].Columns[1].ColumnName;
- comboBox2.ValueMember = mydataset2.Tables["JOB"].Columns[1].ColumnName;
- DataSet mydataset3 = new DataSet();
- sqlcn = mydatacon.getcon();
- cmd = "select ID as 员工号, NAME as 姓名, SEX as 性别, BIRTHDAY as 生日, DEPARTMENT as 所在部门, JOB as 职务, EDU_LEVEL as 受教育程度, SPCIALTY as 专业技能, ADDRESS as 家庭住址, TEL as 联系电话, EMAIL as 电子邮箱, STATE as 当前状态, REMARK as 备注 from [PERSON]";
- myadapter = new SqlDataAdapter(cmd, sqlcn);
- mydataset3.Clear();
- myadapter.Fill(mydataset3, "PERSON");
- dataGridView1.DataSource = mydataset3.Tables["PERSON"];
- //以上这两段相当于对comboBox进行数据绑定,不同的是以这样的方式可以防止数据库变动以及不刷新数据所造成的问题
- }
- private void groupBox1_Enter(object sender, EventArgs e)
- {
- }
- private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
- {
- }
- private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
- {
- int n = dataGridView1.CurrentRow.Index;
- dataGridView1.CurrentCell = dataGridView1[0, n];
- string str = dataGridView1.CurrentCell.Value.ToString();
- sqlcn = mydatacon.getcon();
- sqlcn.Open();
- SqlDataAdapter com = new SqlDataAdapter("select * from [PERSON] where ID='" + str + "'", sqlcn);
- DataSet ds = new DataSet();
- com.Fill(ds);
- foreach (DataRow dr in ds.Tables[0].Rows)
- {
- textBox1.Text = dr["ID"].ToString();
- textBox2.Text = dr["ID"].ToString();
-
- }
- }
- private void textBox2_TextChanged(object sender, EventArgs e)
- {
- }
-
- }
- }