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

📄 distributeemployee.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 DistributeEmployee : Form
    {
        private string temp1 = null;//人员信息
        private string temp2 = null;//部门信息

        private string ID_temp = null;

        public void Set_textBox2(string a)
        {
            this.textBox2.Text = a;//待删除部门的名称
        }

        public void Set_ID_temp(string a)
        {
            ID_temp = a;//保存待删部门的ID
        }

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

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

            //连接数据库(员工)
            SqlConnection New_Myconn = new SqlConnection("Data Source=NUAA\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=aaa");
            SqlCommand New_selectCMD = new SqlCommand("SELECT EmployeeID,Name,DeptID FROM dbo.tblEmployee", New_Myconn);
            New_selectCMD.CommandTimeout = 30;
            SqlDataAdapter New_deptDA = new SqlDataAdapter();
            SqlCommandBuilder modifycommond = new SqlCommandBuilder(New_deptDA);
            New_deptDA.SelectCommand = New_selectCMD;
            New_Myconn.Open();

            DataSet New_deptDS = new DataSet();
            New_deptDA.Fill(New_deptDS, "dbo.tblEmployee");
            /*********************************************************************************/


            int k;//定义临时Int变量K

            //剩余部门名称注入下拉列表
            Dept_comboBox1.Items.Clear();//先清空
            for (k = 0; k < deptDS.Tables["dbo.tblDepartment"].Rows.Count; k++)
            {
                Dept_comboBox1.Items.Add(deptDS.Tables["dbo.tblDepartment"].Rows[k]["DeptName"]);
            }
            /**************************************************************/


            //待删除部门人员注入下拉列表
            Employee_comboBox1.Items.Clear();//先清空
            for (k = 0; k < New_deptDS.Tables["dbo.tblEmployee"].Rows.Count; k++)
            {
                if (New_deptDS.Tables["dbo.tblEmployee"].Rows[k]["DeptID"].ToString() == ID_temp)
                {
                    Employee_comboBox1.Items.Add(New_deptDS.Tables["dbo.tblEmployee"].Rows[k]["Name"]);
                }
            }
            /**************************************************************/
            Employee_comboBox1.Text = null;
            Dept_comboBox1.Text = null;
            Myconn.Close();
            New_Myconn.Close();
        }



        public DistributeEmployee()
        {
            InitializeComponent();
        }

        private void sure_button_Click(object sender, EventArgs e)
        {
            if (Employee_comboBox1.Items.Count == 0)
            {
                this.Hide();
            }
            else
            {
                MessageBox.Show("未完成人员分配");
            }
        }

        private void apply_button_Click(object sender, EventArgs e)
        {
            if (Employee_comboBox1.Text != null && Dept_comboBox1.Text != null)
            {
                temp1 = Employee_comboBox1.Text;
                temp2 = Dept_comboBox1.Text;


                /*********************************************************************************/
                //连接数据库
                /*********************************************************************************/
                //连接数据库(部门)
                SqlConnection Myconn = new SqlConnection("Data Source=NUAA\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=aaa");
                SqlCommand selectCMD = new SqlCommand("SELECT DeptID,DeptName,Description,ManagerID FROM dbo.tblDepartment", Myconn);
                selectCMD.CommandTimeout = 30;
                SqlDataAdapter deptDA = new SqlDataAdapter();
                deptDA.SelectCommand = selectCMD;
                SqlCommandBuilder InsertCommand = new SqlCommandBuilder(deptDA);
                Myconn.Open();

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

                //连接数据库(员工)
                SqlConnection New_Myconn = new SqlConnection("Data Source=NUAA\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=aaa");
                SqlCommand New_selectCMD = new SqlCommand("SELECT EmployeeID,Name,DeptID FROM dbo.tblEmployee", New_Myconn);
                New_selectCMD.CommandTimeout = 30;
                SqlDataAdapter New_deptDA = new SqlDataAdapter();
                SqlCommandBuilder modifycommond = new SqlCommandBuilder(New_deptDA);
                New_deptDA.SelectCommand = New_selectCMD;
                New_Myconn.Open();

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

                /*********************************************************************************/
                int k;//定义临时Int变量K
                int j;
                for (k = 0; k < New_deptDS.Tables["dbo.tblEmployee"].Rows.Count; k++)
                {
                    if (New_deptDS.Tables["dbo.tblEmployee"].Rows[k]["Name"].ToString() == temp1)
                    {
                        for (j = 0; j < deptDS.Tables["dbo.tblDepartment"].Rows.Count; j++)
                        {
                            if (deptDS.Tables["dbo.tblDepartment"].Rows[j]["DeptName"].ToString() == temp2)
                            {
                                New_deptDS.Tables["dbo.tblEmployee"].Rows[k]["DeptID"] = deptDS.Tables["dbo.tblDepartment"].Rows[j]["DeptID"];
                                break;
                            }
                        }
                        break;
                    }
                }
                //更新员工数据库
                New_deptDA.Update(New_deptDS, "dbo.tblEmployee");

                //重写下拉列表****************************************************************

                int k0;//定义临时Int变量K0

                //剩余部门名称注入下拉列表
                Dept_comboBox1.Items.Clear();//先清空
                for (k0 = 0; k0 < deptDS.Tables["dbo.tblDepartment"].Rows.Count; k0++)
                {
                    Dept_comboBox1.Items.Add(deptDS.Tables["dbo.tblDepartment"].Rows[k0]["DeptName"]);
                }
                /**************************************************************/


                //待删除部门人员注入下拉列表
                Employee_comboBox1.Items.Clear();//先清空
                for (k0 = 0; k0 < New_deptDS.Tables["dbo.tblEmployee"].Rows.Count; k0++)
                {
                    if (New_deptDS.Tables["dbo.tblEmployee"].Rows[k0]["DeptID"].ToString() == ID_temp)
                    {
                        Employee_comboBox1.Items.Add(New_deptDS.Tables["dbo.tblEmployee"].Rows[k0]["Name"]);
                    }
                }
                /**********************************************************************************/
                Employee_comboBox1.Text = null;
                Dept_comboBox1.Text = null;
                Myconn.Close();
                New_Myconn.Close();
            }
        }

        private void DistributeEmployee_Load(object sender, EventArgs e)
        {
            
        }
    }
}

⌨️ 快捷键说明

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