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

📄 departmentmanage.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 DepartmentManage : Form
    {
        private string temp = null;
        private string temp1 = null;
        private string temp_common = null;

        private string t = null;

        public string Gettemp()
        {
            return temp;
        }
        public void Settemp(string a)
        {
            temp = a;
        }

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

        public string Gettemp_common()
        {
            return temp_common;
        }
        public void Settemp_common(string a)
        {
            temp_common = a;
        }

        public DepartmentManage()
        {
            InitializeComponent();
            
            //连接数据库(部门)
            
            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;
            Myconn.Open();

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

            DataSet Re_deptDS = new DataSet();
            Re_deptDA.Fill(Re_deptDS, "dbo.tblEmployee");
            Re_Myconn.Close();

            //树的初始化
            if (treeView1.TopNode == null)
            {
                TreeNode node_first = new TreeNode("system");
                this.treeView1.Nodes.Add(node_first);
                treeView1.SelectedNode = node_first;
                
                for (int k = 0; k < deptDS.Tables["dbo.tblDepartment"].Rows.Count; k++)
                {
                    MainForm.center.Setname(deptDS.Tables["dbo.tblDepartment"].Rows[k]["DeptName"].ToString());
                    MainForm.center.SetID_manager((int)deptDS.Tables["dbo.tblDepartment"].Rows[k]["ManagerID"]);

                    for (int j = 0; j < Re_deptDS.Tables["dbo.tblEmployee"].Rows.Count; j++)
                    {
                        if ((int)Re_deptDS.Tables["dbo.tblEmployee"].Rows[j]["EmployeeID"] == (int)deptDS.Tables["dbo.tblDepartment"].Rows[k]["ManagerID"])
                        {
                            MainForm.center.SetName_manager(Re_deptDS.Tables["dbo.tblEmployee"].Rows[j]["Name"].ToString());
                        }
                    }

                    //添加新部门节点
                    TreeNode node = new TreeNode(MainForm.center.Getname());
                    treeView1.SelectedNode.Nodes.Add(node);

                    //添加该部门经理节点
                    TreeNode node1 = new TreeNode(MainForm.center.GetName_manager());
                    TreeNode node2 = new TreeNode(MainForm.center.GetID_manager().ToString());
                    treeView1.SelectedNode = node;
                    this.treeView1.SelectedNode.Nodes.Add(node1);
                    this.treeView1.SelectedNode.Nodes.Add(node2);

                    treeView1.SelectedNode = node_first;
                }
            }
        }

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

        private void AddDepartment_Click(object sender, EventArgs e)
        {
            string name = AddDepartment.Name;
            MainForm.core.OpenForm(name);
        }

        private void DepartmentDetails_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode.Level != 1)
            {
                MessageBox.Show("选择对象无效,请选择部门!");
            }
            else
            {
                //用temp_common存储当前选择的节点的内容
                temp_common = treeView1.SelectedNode.Text;

                string name = DepartmentDetails.Name;
                MainForm.core.OpenForm(name);
            }
        }

        private void Abort_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

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

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
        }

        private void Refresh_tree_Click_1(object sender, EventArgs e)
        {
            if (temp == null || temp_common == null || temp1 == null)
            {
                MessageBox.Show("     Request Department's Name\n or Request Department's description\n or Request Department's Manager");
            }
            
            else
            {
                /*******************************修改数据库*******************************/
                //数据据连结
                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");

                //添加新的行
                DataRow NewRow;
                int i = deptDS.Tables["dbo.tblDepartment"].Rows.Count - 1;
                NewRow = deptDS.Tables["dbo.tblDepartment"].NewRow();

                NewRow["DeptName"] = temp;
                NewRow["Description"] = temp1;
                NewRow["ManagerID"] = temp_common;

                deptDS.Tables["dbo.tblDepartment"].Rows.Add(NewRow);
                deptDA.Update(deptDS, "dbo.tblDepartment");
                Myconn.Close();

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

                DataSet deptDS1 = new DataSet();
                deptDA1.Fill(deptDS1, "dbo.tblEmployee");
                Myconn1.Close();

                string Name_Manager = null;

                for (int j = 0; j < deptDS1.Tables["dbo.tblEmployee"].Rows.Count; j++)
                {
                    if (temp_common == deptDS1.Tables["dbo.tblEmployee"].Rows[j]["EmployeeID"].ToString())
                    {
                        Name_Manager = deptDS1.Tables["dbo.tblEmployee"].Rows[j]["Name"].ToString();
                    }
                }

                /****************************重新连接数据库******************************/
                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");
                /************************************************************************/


                /********************************刷新树**********************************/
                //指定被选中的树结点为根节点
                treeView1.SelectedNode = treeView1.TopNode;
                //添加新部门节点
                TreeNode node = new TreeNode(New_deptDS.Tables["dbo.tblDepartment"].Rows[i + 1]["DeptName"].ToString());
                this.treeView1.SelectedNode.Nodes.Add(node);

                //添加该部门经理节点
                TreeNode node1 = new TreeNode(Name_Manager);
                TreeNode node2 = new TreeNode(New_deptDS.Tables["dbo.tblDepartment"].Rows[i + 1]["ManagerID"].ToString());
                treeView1.SelectedNode = node;
                this.treeView1.SelectedNode.Nodes.Add(node1);
                this.treeView1.SelectedNode.Nodes.Add(node2);

                //获取新增部门的ID
                int id = (int)New_deptDS.Tables["dbo.tblDepartment"].Rows[i + 1]["DeptID"];
                
                Myconn.Close();
                /************************************************************************/


                //改动经理的DeptID
                SqlConnection Re_Myconn = new SqlConnection("Data Source=NUAA\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=aaa");
                SqlCommand Re_selectCMD = new SqlCommand("SELECT EmployeeID,Name,DeptID FROM dbo.tblEmployee", Re_Myconn);
                Re_selectCMD.CommandTimeout = 30;
                SqlDataAdapter Re_deptDA = new SqlDataAdapter();
                SqlCommandBuilder thisCommand = new SqlCommandBuilder(Re_deptDA);
                Re_deptDA.SelectCommand = Re_selectCMD;
                Re_Myconn.Open();

                DataSet Re_deptDS = new DataSet();
                Re_deptDA.Fill(Re_deptDS, "dbo.tblEmployee");

                int k;
                for (k = 0; k < Re_deptDS.Tables["dbo.tblEmployee"].Rows.Count; k++)
                {
                    if (temp_common == Re_deptDS.Tables["dbo.tblEmployee"].Rows[k]["EmployeeID"].ToString())
                    {
                        Re_deptDS.Tables["dbo.tblEmployee"].Rows[k]["DeptID"] = id;
                        break;
                    }
                }
                Re_deptDA.Update(Re_deptDS, "dbo.tblEmployee");

                Re_Myconn.Close();

                temp = null;
                temp_common = null;
                temp1 = null;
                /************************************************************************/
            }
        }

        private void DeleteDepartment_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode.Level != 1)
            {
                MessageBox.Show("选择对象无效,请选择部门!");
            }
            else
            {
                //询问是否进行删除部门操作
                int a = (int)MessageBox.Show(this.treeView1.SelectedNode.Text, "询问:是否进行删除操作", MessageBoxButtons.OKCancel);
                if (a == 1)
                {
                    //允许执行删除命令

                    //连结数据库
                    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);
                    SqlCommandBuilder DeleteCommand = new SqlCommandBuilder(deptDA);
                    Myconn.Open();

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

                    //执行删除
                    temp = this.treeView1.SelectedNode.Text;

                    int i = 0;

                    while (i < deptDS.Tables["dbo.tblDepartment"].Rows.Count)
                    {
                        if (temp == deptDS.Tables["dbo.tblDepartment"].Rows[i]["DeptName"].ToString())
                        {
                            //暂存Department的ID
                            t = deptDS.Tables["dbo.tblDepartment"].Rows[i]["DeptID"].ToString();
                            //删除数据库中数据
                            deptDS.Tables["dbo.tblDepartment"].Rows[i].Delete();
                            deptDA.Update(deptDS, "dbo.tblDepartment");

                            //删除相应树节点
                            this.treeView1.SelectedNode.Remove();
                            break;
                        }
                        i++;
                    }
                    Myconn.Close();

                    //处理员工去留
                    MainForm.core.Open_disEmployee(temp, t);
                }
            }
        }
    }
}

⌨️ 快捷键说明

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