zoukankan      html  css  js  c++  java
  • 无限级分类查找节点的所有子级和父级 (转载)

    一直要实现这个功能,现在看到有人弄出来了转过来先看看。

     

    无限级分类数据表:

    SQL代码:
    create table t_power(
            id 
    int not null IDENTITY(1,1) PRIMARY KEY,      --权限ID
            ParentId 
    int not null default(0),               --父级ID
            Depath 
    int not null default(0),         --级数
            [Name] varchar(
    100default('') not null,                            --名称
            IsMenu 
    int not null default(0),                      ---是否为菜单项
            )
    go
    查找节点的所有子级:

    C#代码:
    /// 
            
    /// 获取子级 返回子级
            
    /// 
            
    /// 父级id
            
    ///子级列表
            public static void GetChild(int parentId,ref IList powerlist )
            {
                            
    string strsql = "select * from t_power where ParentId=" + parentId.ToString();
                
    using (SqlDataReader dr = sql.ExecuteReader(strsql))
                {
                    
    if (!dr.HasRows)
                        
    return;
                    
    while (dr.Read())
                    {
                        PInfop 
    = new PInfo();
                        p.Id 
    = Convert.ToInt32(dr["id"]); ;
                        p.ParentId 
    = Convert.ToInt32(dr["ParentId"]);
                        p.Depath 
    = Convert.ToInt32(dr["Depath"]);
                        p.Name 
    = dr["Name"].ToString();
                        p.IsMenu 
    = Convert.ToInt32(dr["IsMenu"]);
                        powerlist.Add(p);
                        GetChild(Convert.ToInt32(dr[
    "id"]), ref powerlist);
                    }
                }
            }查找所有父级:


    C#代码:
     
    /// 
            
    /// 获取所有祖先
            
    /// 
            
    /// 当前ID
            
    /// 列表
            public static IList GetParentPower(int powerid) {
                IList  powerList 
    = new List();
                           
    string strsql = @"
                    declare @tempTable TABLE
                    (
                     [id] int,
                     ParentId int,
                     Depath int,
                     [Name] varchar(100),
                     IsMenu int,
                     Url varchar(200)
                    )

                    declare @oldId int
                    declare @ID int
                    set @ID={0}
                    set @oldId=@ID
                    while(@ID>0)
                    begin
                        select @ID=ParentId from t_power where ID=@ID;
                            if @oldId=@ID
                                    break
                            if(@ID>0)
                            begin
                        insert into @tempTable select * from t_power where ID=@ID;
                            end
                    end
                    select * from @tempTable order by id asc
                
    ";
                strsql 
    = string.Format(strsql, powerid);
                
    using (SqlDataReader dr = sql.ExecuteReader(strsql))
                {
                    
    while (dr.Read())
                    {
                        PowerInfo p 
    = new PowerInfo();
                        p.Id 
    = Convert.ToInt32(dr["id"]); ;
                        p.ParentId 
    = Convert.ToInt32(dr["ParentId"]);
                        p.Depath 
    = Convert.ToInt32(dr["Depath"]);
                        p.Name 
    = dr["Name"].ToString();
                        p.IsMenu 
    = Convert.ToInt32(dr["IsMenu"]);
                        powerList.Add(p);
                    }
                }
                
    return powerList;
            }

  • 相关阅读:
    设计模式:简单工厂模式
    datav轮播表使用事例
    POI操作Excel常用方法总结 .
    序列图像三维重建 学习过程流水账
    python面向对象编程
    python批量生成word文档
    Linux 网络配置方法 nmtui 配置
    leetcode 剑指 Offer 67. 把字符串转换成整数 & leetcode 8. 字符串转换整数 (atoi)
    leetcode 剑指 Offer 59
    leetcode 剑指 Offer 53
  • 原文地址:https://www.cnblogs.com/fslnet/p/1620449.html
Copyright © 2011-2022 走看看