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;
            }

  • 相关阅读:
    【Java 学习笔记】 HashMultimap(guava)
    [论文笔记] Analysis of Integration Models for Service Composition (ACM workshop, 2002)
    浅谈论文笔记管理方法
    [论文阅读] 论文阅读2篇(COMPSAC09, JNW09)
    [论文笔记] Monitoring Dependencies for SLAs: The MoDe4SLA Approach (SCC, 2008)
    [论文收集] CEC (Commerce and Enterprise Computing) 2009相关论文列表
    [论文笔记] Service Provenance in QoSAware Web Service Runtimes (ICWS, 2009)
    [资料整理] Decentralized Services Orchestration, Choreography相关的几篇论文
    [论文收集] Clustering Algorithm在Web Service领域应用相关论文
    [论文笔记] A minimal role mining method for the web service composition (2009)
  • 原文地址:https://www.cnblogs.com/fslnet/p/1620449.html
Copyright © 2011-2022 走看看