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

ASP.NET
Asp.net 时间操作基类(支持短日期,长日期,时间差)
asp.net 获取机器硬件信息(cpu频率、磁盘可用空间、内存容量等)
asp.net 数据库备份还原(sqlserver+access)
Asp.Net 数据操作类(附通用数据基类)
Asp.net 弹出对话框基类(输出alet警告框)
Asp.net 文件上传类(取得文件后缀名,保存文件,加入文字水印)
Asp.net Socket客户端(远程发送和接收数据)
Asp.net 字符串操作基类(安全,替换,分解等)
Asp.Net数据输出到EXCEL表格中
asp.net Gridview里添加汇总行
asp.net UpdatePanel的简单用法
asp.net ajaxControlToolkit FilteredTextBoxExtender的简单用法
this connector is disabled错误的解决方法
sql事务应用积累
asp.net Page.Controls对象(找到所有服务器控件)
在asp.NET中字符串替换的五种方法
ASP.NET缓存方法分析和实践示例代码
asp.net 在DNN模块开发中遇到的resx怪问题
ASP.NET State service状态服务的问题解决方法
asp.net 结合mysql存储过程进行分页代码

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 86 ::
收藏到网摘: 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服务器与数据库服务器不位于同一台服务器上时,更会受网络影响。