employee_add.cs
上传用户:zhongfine
上传日期:2022-08-01
资源大小:2860k
文件大小:10k
源码类别:

企业管理

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.SqlClient;
  10. using System.IO;
  11. namespace 人事管理系统
  12. {
  13.     public partial class employee_add : Form
  14.     {
  15.         SqlConnection sqlcn = new SqlConnection();
  16.         Datacon mydatacon = new Datacon();
  17.         password mypassword = new password();
  18.         SqlDataAdapter myadapter;
  19.         DataSet mydataset = new DataSet();
  20.         OpenFileDialog mydlg = new OpenFileDialog();
  21.        
  22.         public employee_add()
  23.         {
  24.             InitializeComponent();
  25.             
  26.             sqlcn = mydatacon.getcon();
  27.            
  28.             sqlcn.Open();
  29.             DataSet mydataset = new DataSet();
  30.             String mysql = "查询员工编号 ;";
  31.             SqlCommand sqlcm = new SqlCommand(mysql, sqlcn);
  32.             SqlDataReader dr = sqlcm.ExecuteReader();
  33.             if (dr.Read())
  34.             {
  35.                 String a = dr["COUNTER_VAL"].ToString();
  36.                 int b = int.Parse(a);
  37.                 //或使用int q = int.Parse(dr["COUNTER_VAL"]);也行; 
  38.                 //b = b + 1;
  39.                 this.label10.Text = b.ToString();
  40.             }
  41.             //整个以上这一段读取计数器内员工编号的最新值加1并赋给lable10,用来自动分配员工号
  42.            
  43.         }
  44.         private void employee_add_Load(object sender, EventArgs e)
  45.         {
  46.             #region//界面数据初始化
  47.             sqlcn = mydatacon.getcon();
  48.             string cmd = "select ID as 部门编号, NAME as 部门名称 from [DEPARTMENT]";
  49.             myadapter = new SqlDataAdapter(cmd, sqlcn);
  50.             mydataset.Clear();
  51.             myadapter.Fill(mydataset, "DEPARTMENT");
  52.             comboBox1.DataSource = mydataset.Tables["DEPARTMENT"];
  53.             comboBox1.DisplayMember = mydataset.Tables["DEPARTMENT"].Columns[1].ColumnName;
  54.             comboBox1.ValueMember = mydataset.Tables["DEPARTMENT"].Columns[1].ColumnName;
  55.             DataSet mydataset2 = new DataSet();
  56.             string cnd = "select CODE, DESCRIPTION from [JOB] ";
  57.             myadapter = new SqlDataAdapter(cnd, sqlcn);
  58.             mydataset2.Clear();
  59.             myadapter.Fill(mydataset2, "JOB");
  60.             comboBox6.DataSource = mydataset2.Tables["JOB"];
  61.             comboBox6.DisplayMember = mydataset2.Tables["JOB"].Columns[1].ColumnName;
  62.             comboBox6.ValueMember = mydataset2.Tables["JOB"].Columns[1].ColumnName;
  63.             DataSet mydataset3 = new DataSet();
  64.             string cad = "select CODE, DESCRIPTION from [EDU_LEVEL] ";
  65.             myadapter = new SqlDataAdapter(cad, sqlcn);
  66.             mydataset3.Clear();
  67.             myadapter.Fill(mydataset3, "EDU_LEVEL");
  68.             comboBox3.DataSource = mydataset3.Tables["EDU_LEVEL"];
  69.             comboBox3.DisplayMember = mydataset3.Tables["EDU_LEVEL"].Columns[1].ColumnName;
  70.             comboBox3.ValueMember = mydataset3.Tables["EDU_LEVEL"].Columns[1].ColumnName;
  71.             //以上这三段相当于对comboBox进行数据绑定,不同的是以这样的方式可以防止数据库变动以及不刷新数据所造成的问题
  72.             #endregion
  73.         }
  74.             
  75.         private void button2_Click(object sender, EventArgs e)
  76.         {
  77.             this.Close();
  78.             major mymajor = new major();
  79.            
  80.         }
  81.         private void button1_Click(object sender, EventArgs e)
  82.         {
  83.             if (textBox1.Text == "" || comboBox5.Text == "" || textBox2.Text == "" || comboBox4.Text == "" || comboBox6.Text == "" || comboBox3.Text == "" || comboBox1.Text == "" || comboBox2.Text == "" || textBox6.Text == "")
  84.             {
  85.                 MessageBox.Show("请填写好必填内容!");
  86.             }
  87.             else
  88.             {
  89.                
  90.                 
  91.                 
  92.                 sqlcn = mydatacon.getcon();
  93.                 sqlcn.Open();
  94.                 SqlCommand cmd = new SqlCommand();
  95.                 cmd.Connection = sqlcn;
  96.                 //cmd.CommandText = "更新员工编号计数器";
  97.                 //cmd.ExecuteNonQuery();//给计数器内员工编号的数值加1
  98.                 //SqlCommand cnd = new SqlCommand();
  99.                 //cnd.Connection = sqlcn;
  100.                 //cnd.CommandText = "更新人事变更记录计数器";
  101.                 //cnd.ExecuteNonQuery();//给计数器内人事变更记录的数值加1
  102.               
  103.                 String mysql2 = "查询员工编号";//查询最新员工编号
  104.                 String mysql3 = "查询人事变更记录编号";//查询最新人事变更记录编号
  105.                 SqlCommand sqlcm = new SqlCommand(mysql2, sqlcn);
  106.                 SqlCommand sqlcd = new SqlCommand(mysql3,sqlcn);
  107.                 
  108.                 SqlDataReader dr = sqlcm.ExecuteReader();
  109.                 dr.Read();//读取最新员工编号
  110.                 string num = dr["COUNTER_VAL"].ToString();
  111.                 dr.Close();
  112.                 
  113.                 
  114.                 SqlDataReader da = sqlcd.ExecuteReader();
  115.                 da.Read();//读取最新人事变更记录编号
  116.                 string cod = da["COUNTER_VAL"].ToString();
  117.                 da.Close();
  118.                
  119.                 if (comboBox6.Text == "经理")
  120.                 {
  121.                    
  122.                     SqlCommand jinli = new SqlCommand();
  123.                     jinli.Connection = sqlcn;
  124.                     jinli.CommandText = "select count(*) from [PERSON] where JOB='经理' and DEPARTMENT='"+comboBox1.Text+"'";
  125.                     string t=jinli.ExecuteScalar().ToString();
  126.                     if (t != "0")
  127.                     {
  128.                         MessageBox.Show("该部门经理已存在!请重新选择!");
  129.                         return;
  130.                     }
  131.                     jinli.CommandText = "update [DEPARTMENT] set MANAGER='"+textBox2.Text+"' where NAME='"+comboBox1.Text+"'";
  132.                     jinli.ExecuteNonQuery();//若员工为经理,则将部门表中相应部门的经理更新
  133.                     
  134.                 }
  135.                 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 + "'";
  136.                 sqlcd.ExecuteNonQuery();//执行添加员工的命令
  137.                 SqlCommand sqlcom = new SqlCommand("记录增加员工", sqlcn);
  138.                 sqlcom.CommandType = CommandType.StoredProcedure;
  139.                 sqlcom.Parameters.Add("@code", SqlDbType.TinyInt).Value = cod;
  140.                 sqlcom.Parameters.Add("@description", SqlDbType.VarChar, 50).Value = "新添加了员工号为" + num + "的员工!";
  141.                 sqlcom.ExecuteNonQuery();//将新添加员工的记录写进人事变更记录表里面
  142.                 //cmd.ExecuteNonQuery();
  143.                 ////添加照片
  144.                 Stream ms;
  145.                 byte[] picbyte;
  146.                 if (pictureBox1.Image != null)
  147.                 {
  148.                     ms = mydlg.OpenFile();
  149.                     picbyte = new byte[ms.Length];
  150.                     ms.Position = 0;
  151.                     ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));
  152.                     //SqlConnection sqlcn = new SqlConnection("Data Source=YANG\SQLEXPRESS;Initial Catalog=人事管理系统;Integrated Security=True");
  153.                     //sqlcn.Open();
  154.                     SqlCommand csd = new SqlCommand("update PERSON set PHOTO=@zhaopian where ID='"+num+"'", sqlcn);
  155.                     csd.Parameters.Add("@zhaopian", SqlDbType.Image);
  156.                     csd.Parameters["@zhaopian"].Value = picbyte;
  157.                     csd.ExecuteNonQuery();
  158.                 }
  159.                 MessageBox.Show("员工信息添加成功");
  160.                 String mysql4 = "查询员工编号 ;";
  161.                                  
  162.                 SqlCommand sqlcm3 = new SqlCommand(mysql4, sqlcn);
  163.                 SqlDataReader dr3 = sqlcm3.ExecuteReader();
  164.                 if (dr3.Read())
  165.                 {
  166.                     String a = dr3["COUNTER_VAL"].ToString();
  167.                     int b = int.Parse(a);
  168.                     //b = b + 1;
  169.                     this.label10.Text = b.ToString();
  170.                 }//同最上面一样,读取计数器内员工编号的最新值加1并赋给lable10,用来自动分配员工号,此处相当于刷新
  171.                 sqlcn.Close();
  172.                 textBox1.Clear();
  173.                 textBox2.Clear();
  174.                 textBox4.Clear();
  175.                 textBox5.Clear();
  176.                 textBox6.Clear();
  177.                 textBox7.Clear();//清空已经填写过的信息,便于重新填写
  178.                 pictureBox1.Image = null;
  179.             }
  180.         }
  181.         private void groupBox2_Enter(object sender, EventArgs e)
  182.         {
  183.         }
  184.         private void button3_Click(object sender, EventArgs e)
  185.         {
  186.            
  187.             mydlg.Title = "选择照片";
  188.             mydlg.Filter = "JPEG图像文件(*JPG)|*jpg|所有图像文件(*GIF,*BMP,*JPEG,etc)|*gif;*bmp;*jpeg;*jpeg;*tif;*tiff;*png|所有文件(*.*)|*.*";
  189.             if (mydlg.ShowDialog() == DialogResult.OK)
  190.             {
  191.                
  192.                 Image image = Image.FromFile(mydlg.FileName);
  193.                 pictureBox1.Image = image;
  194.             }
  195.         }
  196.        
  197.         }
  198.     }