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

  • 相关阅读:
    Irrlicht引擎I 配置
    lua学习笔记
    C语言 可变参数
    lua5.2版本在VS2010下的环境搭建
    确实太悠闲了
    【python游戏编程之旅】第四篇---pygame中加载位图与常用的数学函数。
    【LINUX/UNIX网络编程】之简单多线程服务器(多人群聊系统)
    【python游戏编程之旅】第三篇---pygame事件与设备轮询
    【python游戏编程之旅】第二篇--pygame中的IO、数据
    【python游戏编程之旅】第一篇---初识pygame
  • 原文地址:https://www.cnblogs.com/fslnet/p/1620449.html
Copyright © 2011-2022 走看看