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

企业管理

开发平台:

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. namespace 人事管理系统
  11. {
  12.     public partial class job : Form
  13.     {
  14.         Datacon mydatacon = new Datacon();
  15.         SqlConnection sqlcn = new SqlConnection();
  16.         SqlDataAdapter myadapter = new SqlDataAdapter();
  17.         DataSet mydataset = new DataSet();
  18.         SqlCommandBuilder myBuilder = new SqlCommandBuilder();
  19.         public job()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.         private void button1_Click(object sender, EventArgs e)
  24.         {
  25.             sqlcn = mydatacon.getcon();
  26.             string cmd = "select CODE as 职位编号, DESCRIPTION as 职位名称 from [JOB]";
  27.             myadapter = new SqlDataAdapter(cmd, sqlcn);
  28.             mydataset.Clear();
  29.             myadapter.Fill(mydataset, "JOB");
  30.             dataGridView1.DataSource = mydataset.Tables["JOB"];//用来刷新DataGridView里面的数据
  31.             sqlcn.Close();
  32.         }
  33.         //private DataTable comDatatable;
  34.         private void job_Load(object sender, EventArgs e)
  35.         {
  36.             sqlcn = mydatacon.getcon();//调用连接函数
  37.             string cmd = "select CODE as 职位编号, DESCRIPTION as 职位名称 from [JOB]";
  38.             myadapter = new SqlDataAdapter(cmd, sqlcn);
  39.             mydataset.Clear();
  40.             myadapter.Fill(mydataset, "JOB");
  41.             dataGridView1.DataSource = mydataset.Tables["JOB"];
  42.             comboBox2.DataSource = mydataset.Tables["JOB"];
  43.             comboBox2.DisplayMember = mydataset.Tables["JOB"].Columns[1].ColumnName;
  44.             comboBox2.ValueMember = mydataset.Tables["JOB"].Columns[1].ColumnName;
  45.             //在窗体加载的同时填充DataGridView和ComboBox内的数据
  46.             sqlcn.Close();
  47.         }
  48.         private void button2_Click(object sender, EventArgs e)
  49.         {
  50.             myBuilder = new SqlCommandBuilder(myadapter);
  51.             myadapter.Update(mydataset, "JOB");
  52.             MessageBox.Show("保存修改信息成功!");//保存对表格内的修改
  53.             sqlcn.Close();
  54.         }
  55.         private void button3_Click(object sender, EventArgs e)
  56.         {
  57.             this.Close();
  58.         }
  59.         private void button4_Click(object sender, EventArgs e)
  60.         {
  61.             if (this.comboBox1.Text == "删除")
  62.             {
  63.                 sqlcn = mydatacon.getcon();
  64.                 sqlcn.Open();
  65.                 SqlCommand cmd = new SqlCommand();
  66.                 cmd.Connection = sqlcn;
  67.                 SqlCommand cnd = new SqlCommand("查询职位是否存在 '" + this.comboBox2.Text + "'", sqlcn);
  68.                 string q = cnd.ExecuteScalar().ToString();//查询与输入相同的职位是否存在
  69.                 SqlCommand ccd = new SqlCommand("select count(*) from [PERSON] where JOB= '" + this.comboBox2.Text + "'", sqlcn);
  70.                 string t = ccd.ExecuteScalar().ToString();
  71.                 if (q == "0")
  72.                 {
  73.                     MessageBox.Show("您输入的职位不存在,请重新选择!");
  74.                     sqlcn.Close();
  75.                     return;
  76.                 }
  77.                 else
  78.                     if (t != "0")
  79.                     {
  80.                         MessageBox.Show("您想删除的职位当前正在被使用,请确认后再删除!");
  81.                         sqlcn.Close();
  82.                         return;
  83.                     }
  84.                     else
  85.                 {
  86.                     cmd.CommandText = "delete from [JOB] where DESCRIPTION='" + this.comboBox2.Text + "'";
  87.                     cmd.ExecuteNonQuery();//执行删除职位的命令
  88.                    
  89.                     string cad = "select CODE as 职位编号, DESCRIPTION as 职位名称 from [JOB]";
  90.                     myadapter = new SqlDataAdapter(cad, sqlcn);
  91.                     mydataset.Clear();
  92.                     myadapter.Fill(mydataset, "JOB");
  93.                     dataGridView1.DataSource = mydataset.Tables["JOB"];
  94.                     sqlcn.Close();//再次刷新DataGridView内的数据
  95.                    
  96.                 }
  97.             }
  98.             else
  99.             {
  100.                 sqlcn = mydatacon.getcon();
  101.                 sqlcn.Open();
  102.                 SqlCommand cnd = new SqlCommand("查询职位是否存在 '" + this.comboBox2.Text + "'", sqlcn);
  103.                 string q = cnd.ExecuteScalar().ToString();//同上
  104.                 if (q == "0")
  105.                 {
  106.                     SqlCommand cmd = new SqlCommand();
  107.                     cmd.Connection = sqlcn;
  108.                     cmd.CommandText = "insert into [JOB](DESCRIPTION) values('" + this.comboBox2.Text + "')";
  109.                     cmd.ExecuteNonQuery();//执行添加职位命令
  110.                   
  111.                     string cad = "select CODE as 职位编号, DESCRIPTION as 职位名称 from [JOB]";
  112.                     myadapter = new SqlDataAdapter(cad, sqlcn);
  113.                     mydataset.Clear();
  114.                     myadapter.Fill(mydataset, "JOB");
  115.                     dataGridView1.DataSource = mydataset.Tables["JOB"];
  116.                     sqlcn.Close();//再次刷新DataGridView内的数据
  117.                   
  118.                 }
  119.                 else
  120.                 {
  121.                     MessageBox.Show("您输入的职位已存在,请重新输入!");
  122.                     sqlcn.Close();
  123.                     return;
  124.                 }
  125.             }
  126.         }
  127.         private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  128.         {
  129.             int n = dataGridView1.CurrentRow.Index;
  130.             dataGridView1.CurrentCell = dataGridView1[0, n];
  131.             string str = dataGridView1.CurrentCell.Value.ToString();
  132.             sqlcn.Open();
  133.             SqlDataAdapter com = new SqlDataAdapter("select * from [JOB] where CODE='" + str + "'", sqlcn);
  134.             DataSet ds = new DataSet();
  135.             com.Fill(ds);
  136.             foreach (DataRow dr in ds.Tables[0].Rows)
  137.             {
  138.                 comboBox2.Text = dr["DESCRIPTION"].ToString();
  139.             }
  140.             sqlcn.Close();
  141.         }
  142.     }
  143. }