employee_add.cs
上传用户:zhongfine
上传日期:2022-08-01
资源大小:2860k
文件大小:10k
- 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;
- using System.IO;
- namespace 人事管理系统
- {
- public partial class employee_add : Form
- {
- SqlConnection sqlcn = new SqlConnection();
- Datacon mydatacon = new Datacon();
- password mypassword = new password();
- SqlDataAdapter myadapter;
- DataSet mydataset = new DataSet();
- OpenFileDialog mydlg = new OpenFileDialog();
-
- public employee_add()
- {
- InitializeComponent();
-
- sqlcn = mydatacon.getcon();
-
- sqlcn.Open();
- DataSet mydataset = new DataSet();
- String mysql = "查询员工编号 ;";
- SqlCommand sqlcm = new SqlCommand(mysql, sqlcn);
- SqlDataReader dr = sqlcm.ExecuteReader();
- if (dr.Read())
- {
- String a = dr["COUNTER_VAL"].ToString();
- int b = int.Parse(a);
- //或使用int q = int.Parse(dr["COUNTER_VAL"]);也行;
- //b = b + 1;
- this.label10.Text = b.ToString();
- }
- //整个以上这一段读取计数器内员工编号的最新值加1并赋给lable10,用来自动分配员工号
-
- }
- private void employee_add_Load(object sender, EventArgs e)
- {
- #region//界面数据初始化
- 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");
- comboBox6.DataSource = mydataset2.Tables["JOB"];
- comboBox6.DisplayMember = mydataset2.Tables["JOB"].Columns[1].ColumnName;
- comboBox6.ValueMember = mydataset2.Tables["JOB"].Columns[1].ColumnName;
- DataSet mydataset3 = new DataSet();
- string cad = "select CODE, DESCRIPTION from [EDU_LEVEL] ";
- myadapter = new SqlDataAdapter(cad, sqlcn);
- mydataset3.Clear();
- myadapter.Fill(mydataset3, "EDU_LEVEL");
- comboBox3.DataSource = mydataset3.Tables["EDU_LEVEL"];
- comboBox3.DisplayMember = mydataset3.Tables["EDU_LEVEL"].Columns[1].ColumnName;
- comboBox3.ValueMember = mydataset3.Tables["EDU_LEVEL"].Columns[1].ColumnName;
- //以上这三段相当于对comboBox进行数据绑定,不同的是以这样的方式可以防止数据库变动以及不刷新数据所造成的问题
- #endregion
- }
-
- private void button2_Click(object sender, EventArgs e)
- {
- this.Close();
- major mymajor = new major();
-
- }
- private void button1_Click(object sender, EventArgs e)
- {
- if (textBox1.Text == "" || comboBox5.Text == "" || textBox2.Text == "" || comboBox4.Text == "" || comboBox6.Text == "" || comboBox3.Text == "" || comboBox1.Text == "" || comboBox2.Text == "" || textBox6.Text == "")
- {
- MessageBox.Show("请填写好必填内容!");
- }
- else
- {
-
-
-
- sqlcn = mydatacon.getcon();
- sqlcn.Open();
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = sqlcn;
- //cmd.CommandText = "更新员工编号计数器";
- //cmd.ExecuteNonQuery();//给计数器内员工编号的数值加1
- //SqlCommand cnd = new SqlCommand();
- //cnd.Connection = sqlcn;
- //cnd.CommandText = "更新人事变更记录计数器";
- //cnd.ExecuteNonQuery();//给计数器内人事变更记录的数值加1
-
- String mysql2 = "查询员工编号";//查询最新员工编号
- String mysql3 = "查询人事变更记录编号";//查询最新人事变更记录编号
- SqlCommand sqlcm = new SqlCommand(mysql2, sqlcn);
- SqlCommand sqlcd = new SqlCommand(mysql3,sqlcn);
-
- SqlDataReader dr = sqlcm.ExecuteReader();
- dr.Read();//读取最新员工编号
- string num = dr["COUNTER_VAL"].ToString();
- dr.Close();
-
-
- SqlDataReader da = sqlcd.ExecuteReader();
- da.Read();//读取最新人事变更记录编号
- string cod = da["COUNTER_VAL"].ToString();
- da.Close();
-
- if (comboBox6.Text == "经理")
- {
-
- SqlCommand jinli = new SqlCommand();
- jinli.Connection = sqlcn;
- jinli.CommandText = "select count(*) from [PERSON] where JOB='经理' and DEPARTMENT='"+comboBox1.Text+"'";
- string t=jinli.ExecuteScalar().ToString();
- if (t != "0")
- {
- MessageBox.Show("该部门经理已存在!请重新选择!");
- return;
- }
- jinli.CommandText = "update [DEPARTMENT] set MANAGER='"+textBox2.Text+"' where NAME='"+comboBox1.Text+"'";
- jinli.ExecuteNonQuery();//若员工为经理,则将部门表中相应部门的经理更新
-
- }
- sqlcd.CommandText = "添加员工 '" + num + "','" + mypassword.IN(this.textBox1.Text) + "','" + this.comboBox5.Text + "','" + this.textBox2.Text + "','" + this.comboBox4.Text + "','" + this.dateTimePicker1.Text + "','" + comboBox1.Text + "','" + comboBox6.Text + "','" + comboBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + comboBox2.Text + "','" + textBox8.Text + "'";
- sqlcd.ExecuteNonQuery();//执行添加员工的命令
- SqlCommand sqlcom = new SqlCommand("记录增加员工", sqlcn);
- sqlcom.CommandType = CommandType.StoredProcedure;
- sqlcom.Parameters.Add("@code", SqlDbType.TinyInt).Value = cod;
- sqlcom.Parameters.Add("@description", SqlDbType.VarChar, 50).Value = "新添加了员工号为" + num + "的员工!";
- sqlcom.ExecuteNonQuery();//将新添加员工的记录写进人事变更记录表里面
- //cmd.ExecuteNonQuery();
- ////添加照片
- Stream ms;
- byte[] picbyte;
- if (pictureBox1.Image != null)
- {
- ms = mydlg.OpenFile();
- picbyte = new byte[ms.Length];
- ms.Position = 0;
- ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));
- //SqlConnection sqlcn = new SqlConnection("Data Source=YANG\SQLEXPRESS;Initial Catalog=人事管理系统;Integrated Security=True");
- //sqlcn.Open();
- SqlCommand csd = new SqlCommand("update PERSON set PHOTO=@zhaopian where ID='"+num+"'", sqlcn);
- csd.Parameters.Add("@zhaopian", SqlDbType.Image);
- csd.Parameters["@zhaopian"].Value = picbyte;
- csd.ExecuteNonQuery();
- }
- MessageBox.Show("员工信息添加成功");
- String mysql4 = "查询员工编号 ;";
-
- SqlCommand sqlcm3 = new SqlCommand(mysql4, sqlcn);
- SqlDataReader dr3 = sqlcm3.ExecuteReader();
- if (dr3.Read())
- {
- String a = dr3["COUNTER_VAL"].ToString();
- int b = int.Parse(a);
- //b = b + 1;
- this.label10.Text = b.ToString();
- }//同最上面一样,读取计数器内员工编号的最新值加1并赋给lable10,用来自动分配员工号,此处相当于刷新
- sqlcn.Close();
- textBox1.Clear();
- textBox2.Clear();
- textBox4.Clear();
- textBox5.Clear();
- textBox6.Clear();
- textBox7.Clear();//清空已经填写过的信息,便于重新填写
- pictureBox1.Image = null;
- }
- }
- private void groupBox2_Enter(object sender, EventArgs e)
- {
- }
- private void button3_Click(object sender, EventArgs e)
- {
-
- mydlg.Title = "选择照片";
- mydlg.Filter = "JPEG图像文件(*JPG)|*jpg|所有图像文件(*GIF,*BMP,*JPEG,etc)|*gif;*bmp;*jpeg;*jpeg;*tif;*tiff;*png|所有文件(*.*)|*.*";
- if (mydlg.ShowDialog() == DialogResult.OK)
- {
-
- Image image = Image.FromFile(mydlg.FileName);
- pictureBox1.Image = image;
- }
- }
-
- }
- }