job.cs
上传用户:zhongfine
上传日期:2022-08-01
资源大小:2860k
文件大小:6k
- 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 job : Form
- {
- Datacon mydatacon = new Datacon();
- SqlConnection sqlcn = new SqlConnection();
- SqlDataAdapter myadapter = new SqlDataAdapter();
- DataSet mydataset = new DataSet();
- SqlCommandBuilder myBuilder = new SqlCommandBuilder();
- public job()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- sqlcn = mydatacon.getcon();
- string cmd = "select CODE as 职位编号, DESCRIPTION as 职位名称 from [JOB]";
- myadapter = new SqlDataAdapter(cmd, sqlcn);
- mydataset.Clear();
- myadapter.Fill(mydataset, "JOB");
- dataGridView1.DataSource = mydataset.Tables["JOB"];//用来刷新DataGridView里面的数据
- sqlcn.Close();
- }
- //private DataTable comDatatable;
- private void job_Load(object sender, EventArgs e)
- {
- sqlcn = mydatacon.getcon();//调用连接函数
- string cmd = "select CODE as 职位编号, DESCRIPTION as 职位名称 from [JOB]";
- myadapter = new SqlDataAdapter(cmd, sqlcn);
- mydataset.Clear();
- myadapter.Fill(mydataset, "JOB");
- dataGridView1.DataSource = mydataset.Tables["JOB"];
- comboBox2.DataSource = mydataset.Tables["JOB"];
- comboBox2.DisplayMember = mydataset.Tables["JOB"].Columns[1].ColumnName;
- comboBox2.ValueMember = mydataset.Tables["JOB"].Columns[1].ColumnName;
- //在窗体加载的同时填充DataGridView和ComboBox内的数据
- sqlcn.Close();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- myBuilder = new SqlCommandBuilder(myadapter);
- myadapter.Update(mydataset, "JOB");
- MessageBox.Show("保存修改信息成功!");//保存对表格内的修改
- sqlcn.Close();
- }
- private void button3_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- 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();//查询与输入相同的职位是否存在
- SqlCommand ccd = new SqlCommand("select count(*) from [PERSON] where JOB= '" + 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 [JOB] where DESCRIPTION='" + this.comboBox2.Text + "'";
- cmd.ExecuteNonQuery();//执行删除职位的命令
-
- string cad = "select CODE as 职位编号, DESCRIPTION as 职位名称 from [JOB]";
- myadapter = new SqlDataAdapter(cad, sqlcn);
- mydataset.Clear();
- myadapter.Fill(mydataset, "JOB");
- dataGridView1.DataSource = mydataset.Tables["JOB"];
- sqlcn.Close();//再次刷新DataGridView内的数据
-
- }
- }
- else
- {
- sqlcn = mydatacon.getcon();
- sqlcn.Open();
- SqlCommand cnd = new SqlCommand("查询职位是否存在 '" + this.comboBox2.Text + "'", sqlcn);
- string q = cnd.ExecuteScalar().ToString();//同上
- if (q == "0")
- {
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = sqlcn;
- cmd.CommandText = "insert into [JOB](DESCRIPTION) values('" + this.comboBox2.Text + "')";
- cmd.ExecuteNonQuery();//执行添加职位命令
-
- string cad = "select CODE as 职位编号, DESCRIPTION as 职位名称 from [JOB]";
- myadapter = new SqlDataAdapter(cad, sqlcn);
- mydataset.Clear();
- myadapter.Fill(mydataset, "JOB");
- dataGridView1.DataSource = mydataset.Tables["JOB"];
- sqlcn.Close();//再次刷新DataGridView内的数据
-
- }
- 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 [JOB] where CODE='" + str + "'", sqlcn);
- DataSet ds = new DataSet();
- com.Fill(ds);
- foreach (DataRow dr in ds.Tables[0].Rows)
- {
- comboBox2.Text = dr["DESCRIPTION"].ToString();
- }
- sqlcn.Close();
- }
- }
- }