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

企业管理

开发平台:

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