当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > C# 无限级分类的实现

ASP.NET
析构函数的作用 什么是析构函数
asp.net下Request.QueryString取不到值的解决方法
设置DropDownList的当前选项
生成多字段排序分页的SQL的通用类
注册表中存储数据库链接字符串的方法
asp.net下百度的编码和解码
asp.net 操作excel的实现代码
用ASP.NET做的个性化的邮件发送系统
DotNet2.0 生成网站的测试
ASP.net 验证码实现代码(C#)
C#中string与byte[]的转换帮助类-.NET教程,C#语言
把jQuery的each(callback)方法移植到c#中
asp.net对URL含有中文参数的转换
C#语言初级入门介绍
C#编码好习惯小结
C#声明方法实例说明
C#使用正则表达式实例
C#列出局域网中可用SQL Server服务器(续)
将DataRow转成指定类型的类,并返回这个类的对象(带值)
asp.net OleDbCommand 的用法

ASP.NET 中的 C# 无限级分类的实现


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 96 ::
收藏到网摘: n/a

采用存储过程实现递归逻辑,直接返回子分类列表的方式应该有更好的性能,尤其是Web服务器与数据库服务器不位于同一台服务器上时,更会受网络影响。 数据库表:CategoryInfo
字段名 类型
ciID int //记录序号,自增量
ciName nvarchar(20) //分类名
ciParent int //父分类序号
ciLayer int //所处的层次
ciDescription nvarchar(200) //对分类的描述
分类的类设计
public class CategoryInfo
{
private int ciID;//分类ID
private string ciName;//分类名
private int ciParent;//分类的父分类ID
private string ciDescription;//分类描述
private int ciLayer;//分类所属层次
//构造函数
public CategoryInfo() { }
public CategoryInfo(int cID, string cName, int cParent, string cDescription, int cLayer)
{
this.ciID = cID;
this.ciName = cName;
this.ciParent = cParent;
this.ciDescription = cDescription;
this.ciLayer = cLayer;
}
//属性
public int CategoryID
{
get{ return ciID;}
set { ciID = value;}
}
public string CategoryName
{
get{ return ciName;}
set { ciName = value; }
}
public int CategoryParent
{
get{ return ciParent;}
set { ciParent = value; }
}
public string CategoryDescription
{
get { return ciDescription; }
set { ciDescription = value; }
}
public int CategoryLayer
{
get { return ciLayer; }
set { ciLayer = value; }
}
}
获取子分类的存储过程
CREATE PROCEDURE [dbo].[category_getChild]
@cName nvarchar(20)
AS
BEGIN
DECLARE @tmpID int
SELECT @tmpID=ciID FROM CategoryInfo
WHERE RTRIM(ciName) = RTRIM(@cName)
if(@tmpID IS NOT NULL)
SELECT * FROM CategoryInfo
WHERE ciParent = @tmpID
ORDER BY ciLayer
END
获取子分类的函数
public IList<CategoryInfo> GetChildCategories(IList<CategoryInfo> cInfos,string cName)
{
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("category_getChild", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter(PARAM_CNAME, SqlDbType.NVarChar, 20));
cmd.Parameters[PARAM_CNAME].Value = cName;
IList<string> tmpNames = new List<string>(); //临时存储获取的子
try
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
CategoryInfo cInfo = new CategoryInfo(
(int)reader["ciID"],
reader["ciName"].ToString(),
(int)reader["ciParent"],
reader["ciDescription"].ToString(),
(int)reader["ciLayer"]
);
string tmpName = reader["ciName"].ToString();
cInfos.Add(cInfo);//添加获取到的分类到cInfos
tmpNames.Add(tmpName);//添加获取到的子分类名到tmpNames
}
}
}
catch
{
throw new ApplicationException("获取分类出错!");
}
finally
{
con.Close();
}
foreach(string c in tmpNames)
{
cInfos = GetChildCategories(cInfos,c); //递归运算。继续获取子分类
}
return cInfos;
}
说明:在该函数中,tmpNames如果换成是IList<CategoryInfo>,即它添加的元素与cInfos是一样的时,如果要移除其中的一项,则cInfos中会同时移除一项。因为CategoryInfo是类,是引用类型的,而非值类型。所以tmpNames采用了string类型,以避免这个问题。
对上面这个函数直接调用还稍嫌麻烦,上层程序还需要建立一个IList<CategoryInfo>对象,因此可以增加一个函数来调用上面的函数。这样,上层程序只需要提供分类名,以及是否包含本级分类两个参数就可以了。
//获取子分类,其中布尔参数表示是否包含本级分类
public IList<CategoryInfo> GetCategories( string cName, bool IsIncludeSelf)
{
IList<CategoryInfo> cInfos = new List<CategoryInfo>();
cInfos = GetChildCategories(cInfos, cName);
if (IsIncludeSelf == true)
{
cInfos.Insert(0, GetByName(cName));//根据名字获取分类,这个很简单,本文略。
}
return cInfos;
}
注意:采用这种方式时,WEB服务器获取子分类时要在数据库服务器之间有多次往返,降低了性能。采用存储过程实现递归逻辑,直接返回子分类列表的方式应该有更好的性能,尤其是Web服务器与数据库服务器不位于同一台服务器上时,更会受网络影响。