无限级分类查找节点的所有子级和父级 (转载)

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

 

无限级分类数据表:

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

posted @ 2009-12-09 16:46  fsl  阅读(3900)  评论(0编辑  收藏  举报