department.cs
上传用户:zhongfine
上传日期:2022-08-01
资源大小:2860k
文件大小:7k
- 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 department : Form
- {
- Datacon mydatacon = new Datacon();
- SqlConnection sqlcn = new SqlConnection();
- SqlDataAdapter myadapter = new SqlDataAdapter();
- DataSet mydataset = new DataSet();
- SqlCommandBuilder myBuilder = new SqlCommandBuilder();
- public department()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
-
- sqlcn = mydatacon.getcon();
- string cmd = "select ID as 部门编号, NAME as 部门名称,MANAGER as 部门经理, INTRO as 简介 from [DEPARTMENT]";
- myadapter = new SqlDataAdapter(cmd, sqlcn);
- mydataset.Clear();
- sqlcn.Open();
- myadapter.Fill(mydataset, "DEPARTMENT");
- dataGridView1.DataSource = mydataset.Tables["DEPARTMENT"];//刷新dataGridView1内部门的最新数据
- sqlcn.Close();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- sqlcn = mydatacon.getcon();
- sqlcn.Open();
- myBuilder = new SqlCommandBuilder(myadapter);
- myadapter.Update(mydataset, "DEPARTMENT");//将对表格中进行的修改保存进数据库
- MessageBox.Show("保存修改信息成功!");
- sqlcn.Close();
- }
-
- private void button3_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- private void department_Load(object sender, EventArgs e)
- {
- sqlcn = mydatacon.getcon();
- string cmd = "select ID as 部门编号, NAME as 部门名称,MANAGER as 部门经理, INTRO as 简介 from [DEPARTMENT]";
- myadapter = new SqlDataAdapter(cmd, sqlcn);
- mydataset.Clear();
- sqlcn.Open();
- myadapter.Fill(mydataset, "DEPARTMENT");
- dataGridView1.DataSource = mydataset.Tables["DEPARTMENT"];
- comboBox2.DataSource = mydataset.Tables["DEPARTMENT"];
- comboBox2.DisplayMember = mydataset.Tables["DEPARTMENT"].Columns[1].ColumnName;
- comboBox2.ValueMember = mydataset.Tables["DEPARTMENT"].Columns[1].ColumnName;
- sqlcn.Close();
- //整个上面表示在窗体加载的同时,对dataGridView1与comboBox2进行填充数据
- }
- private void button4_Click(object sender, EventArgs e)
- {
- if (this.comboBox1.Text == "删除")
- {
- sqlcn = mydatacon.getcon();
-
- sqlcn.Open();
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = sqlcn;
- SqlCommand cnd = new SqlCommand("查询部门是否存在 '" + this.comboBox2.Text + "'", sqlcn);
- string q = cnd.ExecuteScalar().ToString();//查询该部门是否存在,用q来记录
- SqlCommand ccd = new SqlCommand("select count(*) from [PERSON] where DEPARTMENT='" + this.comboBox2.Text + "'",sqlcn);
- string t = ccd.ExecuteScalar().ToString();
- if (q == "0")
- {
- MessageBox.Show("您输入的部门不存在,请重新选择!");
- sqlcn.Close();
- return;
- }
- else
- if (t != "0")
- {
- MessageBox.Show("您想删除的部门当前正在被使用,请确认后再删除!");
- sqlcn.Close();
- return;
- }
- else
- {
- cmd.CommandText = "delete from [DEPARTMENT] where NAME='" + this.comboBox2.Text + "'";
- cmd.ExecuteNonQuery();//执行删除部门的命令
-
- string cad = "select ID as 部门编号, NAME as 部门名称,MANAGER as 部门经理, INTRO as 简介 from [DEPARTMENT]";
- myadapter = new SqlDataAdapter(cad, sqlcn);
- mydataset.Clear();
- myadapter.Fill(mydataset, "DEPARTMENT");
- dataGridView1.DataSource = mydataset.Tables["DEPARTMENT"];//刷新dataGridView1内部门的最新数据
- sqlcn.Close();
-
- }
- }
- else
- {
- sqlcn = mydatacon.getcon();
-
- sqlcn.Open();
- SqlCommand cnd = new SqlCommand("查询部门是否存在 '" + this.comboBox2.Text + "'", sqlcn);
- string q = cnd.ExecuteScalar().ToString();//查询该部门是否存在,用q来记录
- if (q == "0")
- {
- SqlCommand cmd = new SqlCommand();
-
- cmd.Connection = sqlcn;
- cmd.CommandText = "insert into [DEPARTMENT](NAME) values('" + this.comboBox2.Text + "')";
- cmd.ExecuteNonQuery();//执行添加部门的命令
- string cad = "select ID as 部门编号, NAME as 部门名称,MANAGER as 部门经理, INTRO as 简介 from [DEPARTMENT]";
- myadapter = new SqlDataAdapter(cad, sqlcn);
- mydataset.Clear();
- myadapter.Fill(mydataset, "DEPARTMENT");
- dataGridView1.DataSource = mydataset.Tables["DEPARTMENT"];//刷新dataGridView1内部门的最新数据
- sqlcn.Close();
-
- }
- else
- {
- MessageBox.Show("您输入的部门已存在,请重新输入!");
- sqlcn.Close();
- return;
- }
- }
-
- }
- 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.Open();
- SqlDataAdapter com = new SqlDataAdapter("select * from [DEPARTMENT] where ID='" + str + "'",sqlcn);
- DataSet ds = new DataSet();
- com.Fill(ds);
- foreach (DataRow dr in ds.Tables[0].Rows)
- {
- comboBox2.Text= dr["NAME"].ToString();
-
- }
- sqlcn.Close();
- }
-
- }
- }