⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 departmentdetails.cs

📁 实现企业的人事管理
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace renshiguanli
{
    public partial class DepartmentDetails : Form
    {
        private string temp1;
        private int id;

        private string temp_name = null;
        private string temp_login = null;
        private string temp_email = null;

        public void Set_Dept_comboBox1()
        {
            //连接数据库(部门)
            SqlConnection New_Myconn = new SqlConnection("Data Source=NUAA\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=aaa");
            SqlCommand New_selectCMD = new SqlCommand("SELECT DeptID,DeptName,Description,ManagerID FROM dbo.tblDepartment", New_Myconn);
            New_selectCMD.CommandTimeout = 30;
            SqlDataAdapter New_deptDA = new SqlDataAdapter();
            New_deptDA.SelectCommand = New_selectCMD;
            SqlCommandBuilder New_InsertCommand = new SqlCommandBuilder(New_deptDA);
            New_Myconn.Open();

            DataSet New_deptDS = new DataSet();
            New_deptDA.Fill(New_deptDS, "dbo.tblDepartment");
            New_Myconn.Close();

            Dept_comboBox1.Items.Clear();//先将Dept_comboBox1中内容清空

            int k;
            for (k = 0; k < New_deptDS.Tables["dbo.tblDepartment"].Rows.Count; k++)
            {
                Dept_comboBox1.Items.Add(New_deptDS.Tables["dbo.tblDepartment"].Rows[k]["DeptName"]);
            }
            Dept_comboBox1.Text = null;
        }

        public void dataGridView1_load()
        {
            //第一次连接数据库(部门)
            SqlConnection New_Myconn = new SqlConnection("Data Source=NUAA\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=aaa");
            SqlCommand New_selectCMD = new SqlCommand("SELECT DeptID,DeptName,Description,ManagerID FROM dbo.tblDepartment", New_Myconn);
            New_selectCMD.CommandTimeout = 30;
            SqlDataAdapter New_deptDA = new SqlDataAdapter();
            New_deptDA.SelectCommand = New_selectCMD;
            SqlCommandBuilder New_InsertCommand = new SqlCommandBuilder(New_deptDA);
            New_Myconn.Open();

            DataSet New_deptDS = new DataSet();
            New_deptDA.Fill(New_deptDS, "dbo.tblDepartment");
            New_Myconn.Close();

            int k;
            for (k = 0; k < New_deptDS.Tables["dbo.tblDepartment"].Rows.Count; k++)
            {
                if (temp1 == New_deptDS.Tables["dbo.tblDepartment"].Rows[k]["DeptName"].ToString())
                {
                    id = (int)New_deptDS.Tables["dbo.tblDepartment"].Rows[k]["DeptID"];
                    break;
                }
            }

            //第二次连接数据库(人员)
            SqlConnection Myconn = new SqlConnection("Data Source=NUAA\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=aaa");
            SqlCommand selectCMD = new SqlCommand("SELECT  DeptID,LoginName,Name,Email FROM dbo.tblEmployee", Myconn);
            selectCMD.CommandTimeout = 30;
            SqlDataAdapter deptDA = new SqlDataAdapter();
            deptDA.SelectCommand = selectCMD;
            Myconn.Open();

            DataSet deptDS = new DataSet();
            deptDA.Fill(deptDS, "dbo.tblEmployee");
            Myconn.Close();

            //把人员的部分信息填入表
            dataGridView1.Rows.Clear();//在填入数据之前 清空原来的信息
            int i;

            for (k = 0, i = 0; k < deptDS.Tables["dbo.tblEmployee"].Rows.Count; k++)
            {//找到该部门的员工
                
                if ((int)deptDS.Tables["dbo.tblEmployee"].Rows[k]["DeptID"] == id)
                {
                    dataGridView1.Rows.Add();

                    dataGridView1["Employee_Name", i].Value = deptDS.Tables["dbo.tblEmployee"].Rows[k]["Name"];
                    dataGridView1["Employee_Login", i].Value = deptDS.Tables["dbo.tblEmployee"].Rows[k]["LoginName"];
                    dataGridView1["Employee_Email", i].Value = deptDS.Tables["dbo.tblEmployee"].Rows[k]["Email"];
                    dataGridView1["DeptID", i].Value = deptDS.Tables["dbo.tblEmployee"].Rows[k]["DeptID"];
                    i++;
                }
            }
            //统计部门人数
            this.number.Text = i.ToString();
            //填充下拉列表
            Set_Dept_comboBox1();
        }

        public void Settemp1(string a)
        {
            temp1 = a;
        }
        public string Gettemp1()
        {
            return temp1;
        }

        public void SetLabel2_text()
        {
            this.name_dept.Text = temp1;
            dataGridView1_load();
        }

        public DepartmentDetails()
        {
            InitializeComponent();
        }

        private void DepartmentDetails_Load(object sender, EventArgs e)
        {

        }

        private void Sure_Click(object sender, EventArgs e)
        {
            //清除信息
            this.name_dept.Text = null;
            this.EmployeeName.Text = null;
            this.EmployeeLand.Text = null;
            this.EmployeeEmail.Text = null;
            temp_name = null;
            temp_login = null;
            temp_email = null;
            this.Hide();
        }

        private void name_dept_TextChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                temp_name = dataGridView1["Employee_Name", e.RowIndex].Value.ToString();
                this.EmployeeName.Text = temp_name;
                temp_login = dataGridView1["Employee_Login", e.RowIndex].Value.ToString();
                this.EmployeeLand.Text = temp_login;
                temp_email = dataGridView1["Employee_Email", e.RowIndex].Value.ToString();
                this.EmployeeEmail.Text = temp_email;
            }
        }

        private void apply_button_Click(object sender, EventArgs e)
        {
            if (EmployeeName.Text != null && EmployeeName.Text != "" && Dept_comboBox1.Text != null && Dept_comboBox1.Text != "")
            {
                //连接数据库(部门)
                SqlConnection New_Myconn = new SqlConnection("Data Source=NUAA\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=aaa");
                SqlCommand New_selectCMD = new SqlCommand("SELECT DeptID,DeptName,Description,ManagerID FROM dbo.tblDepartment", New_Myconn);
                New_selectCMD.CommandTimeout = 30;
                SqlDataAdapter New_deptDA = new SqlDataAdapter();
                New_deptDA.SelectCommand = New_selectCMD;
                SqlCommandBuilder New_InsertCommand = new SqlCommandBuilder(New_deptDA);
                New_Myconn.Open();

                DataSet New_deptDS = new DataSet();
                New_deptDA.Fill(New_deptDS, "dbo.tblDepartment");

                //连接数据库(人员)
                SqlConnection Myconn = new SqlConnection("Data Source=NUAA\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=aaa");
                SqlCommand selectCMD = new SqlCommand("SELECT  EmployeeID,DeptID,LoginName,Name,Email FROM dbo.tblEmployee", Myconn);
                selectCMD.CommandTimeout = 30;
                SqlDataAdapter deptDA = new SqlDataAdapter();
                deptDA.SelectCommand = selectCMD;
                SqlCommandBuilder thisCommand = new SqlCommandBuilder(deptDA);
                Myconn.Open();

                DataSet deptDS = new DataSet();
                deptDA.Fill(deptDS, "dbo.tblEmployee");

                int k;//定义临时Int变量K
                int j;
                for (k = 0; k < deptDS.Tables["dbo.tblEmployee"].Rows.Count; k++)
                {
                    if (deptDS.Tables["dbo.tblEmployee"].Rows[k]["Name"].ToString() == EmployeeName.Text)
                    {
                        for (j = 0; j < New_deptDS.Tables["dbo.tblDepartment"].Rows.Count; j++)
                        {
                            if (New_deptDS.Tables["dbo.tblDepartment"].Rows[j]["DeptName"].ToString() == Dept_comboBox1.Text)
                            {
                                deptDS.Tables["dbo.tblEmployee"].Rows[k]["DeptID"] = New_deptDS.Tables["dbo.tblDepartment"].Rows[j]["DeptID"];
                                break;
                            }
                        }
                        break;
                    }
                }
                //更新员工数据库
                deptDA.Update(deptDS, "dbo.tblEmployee");//相应数据库需要主键值
                New_Myconn.Close();
                Myconn.Close();
                //重新装载
                Dept_comboBox1.Items.Clear();
                dataGridView1_load();
            }
            else
            {
                MessageBox.Show("请确认所选员工以及所选部门不为空!");
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -