C# 用户权限管理

int YongHuID = 0;
        int myop = 0;
        int KaiGuan = 0;
        DataTable dtPiPeiMoKuai = new DataTable();
        BLL系统维护.frm_QuanXianSheZhi.frm_QuanXianSheZhiClient myfrm_QuanXianSheZhiClient = new BLL系统维护.frm_QuanXianSheZhi.frm_QuanXianSheZhiClient();

private void trv_YuanGong_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            KaiGuan = 0;
            YongHuID = Convert.ToInt32(e.Node.Name);
            try
            {
                dtPiPeiMoKuai = myfrm_QuanXianSheZhiClient.trv_YuanGong_NodeMouseClick_SelectQuanXian(YongHuID).Tables[0];
            }
            catch { }
            trv_MoKuai.Nodes.Clear();
            AddTree2(7, (TreeNode)null);
            //trv_MoKuai.ExpandAll();
        }

        #region 循环递归提取数据(全部模块)
        public void AddTree2(int ParentID, TreeNode pNode) // 递归,自己调用自己
        {
            DataTable dtTree = myfrm_QuanXianSheZhiClient.trv_YuanGong_NodeMouseClick_SelectMoKuai().Tables[0];
            DataView dvTree = new DataView(dtTree); //就是dtTree = dsFrame.Tables[0];
            string Fstr = "模块Far_ID =" + ParentID;
            dvTree.RowFilter = Fstr;
            foreach (DataRowView Row in dvTree)
            {
                TreeNode Node = new TreeNode();
                if (pNode == null)                  //处理主节点
                {
                    Node.Name = Row["模块ID"].ToString().Trim(); //这里+了2个值分别到Name和Text,可随便
                    Node.Text = Row["模块名称"].ToString().Trim();
                    trv_MoKuai.Nodes.Add(Node);
                    for (int i = 0; i < dtPiPeiMoKuai.Rows.Count; i++)
                    {
                        if (Node.Name == dtPiPeiMoKuai.Rows[i][2].ToString().Trim())//判断用户是否拥有该权限
                        {
                            Node.Checked = true;//拥有则打 √
                        }
                    }
                    AddTree2(Int32.Parse(Row["模块ID"].ToString()), Node); //递归
                }
                else //处理子节点
                {
                    Node.Name = Row["模块ID"].ToString();
                    Node.Text = Row["模块名称"].ToString();
                    pNode.Nodes.Add(Node);
                    for (int i = 0; i < dtPiPeiMoKuai.Rows.Count; i++)
                    {
                        if (Node.Name == dtPiPeiMoKuai.Rows[i][2].ToString().Trim())
                        {
                            Node.Checked = true;
                        }
                    }
                    AddTree2(Int32.Parse(Row["模块ID"].ToString()), Node);
                }
                Node.Tag = Row["模块ID"];  //获取当前节点数据(ID)
            }
        }
        #endregion

        private void btn_BaoCun_Click(object sender, EventArgs e)
        {
            myop = 0;
            for (int i = 0; i < trv_MoKuai.Nodes.Count; i++)
            {
                Recursion(trv_MoKuai.Nodes[i]);
            }
            if (myop > 0)
            {
                MessageBox.Show("操作成功!","提示:");
            }
        }

        void Recursion(TreeNode TN)//递归
        {
            foreach (TreeNode tn in TN.Nodes )
            {
                int MoKuaiID = 0;//模块ID
                int QuanXianID = 0;//权限ID
                bool bolYouXiaoFou = false;//权限有效否
                //操作判断
                MoKuaiID = Convert.ToInt32(tn.Name);//获取模块ID
                try
                {   //检测是否存在权限ID
                    QuanXianID = Convert.ToInt32(myfrm_QuanXianSheZhiClient.btn_BaoCun_Click_SelectQuanXian(YongHuID, MoKuaiID).Tables[0].Rows[0][0].ToString().Trim());
                }
                catch { }
                if (QuanXianID != 0)//若存在权限ID
                {
                    if (tn.Checked)//若该节点被选中
                    {
                        bolYouXiaoFou = true;//则将权限修改为"有效"
                        myop = myfrm_QuanXianSheZhiClient.btn_BaoCun_Click_UpdateQuanXian(QuanXianID, bolYouXiaoFou);
                    }
                    else
                    {
                        bolYouXiaoFou = false;//否则则将权限修改为"无效"
                        myop = myfrm_QuanXianSheZhiClient.btn_BaoCun_Click_UpdateQuanXian(QuanXianID, bolYouXiaoFou);
                    }
                }
                else//若不存在权限ID
                {
                    if (tn.Checked)//若该节点被选中
                    {   //则新增权限
                        myop = myfrm_QuanXianSheZhiClient.btn_BaoCun_Click_InsertQuanXian(YongHuID, MoKuaiID);
                    }
                    else
                    {
                        //神马都不做
                    }
                }
                Recursion(tn);
            }
            
        }


你可能感兴趣的:(C#,.Net,C/S架构,数据库,编程语言)