无限级分类查找节点的所有子级和父级
Code
无限级分类数据表:
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(100) default('') 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;
}
本篇文章来源于 www.shenjk.com 原文链接:http://www.shenjk.com/details/651.html
无限级分类数据表:
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(100) default('') 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;
}
本篇文章来源于 www.shenjk.com 原文链接:http://www.shenjk.com/details/651.html