C# 無限級分類的實(shí)現(xiàn)
字段名 類型
ciID int //記錄序號,自增量
ciName nvarchar(20) //分類名
ciParent int //父分類序號
ciLayer int //所處的層次
ciDescription nvarchar(200) //對分類的描述
分類的類設(shè)計(jì)
public class CategoryInfo
{
private int ciID;//分類ID
private string ciName;//分類名
private int ciParent;//分類的父分類ID
private string ciDescription;//分類描述
private int ciLayer;//分類所屬層次
//構(gòu)造函數(shù)
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
獲取子分類的函數(shù)
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>(); //臨時(shí)存儲獲取的子
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); //遞歸運(yùn)算。繼續(xù)獲取子分類
}
return cInfos;
}
說明:在該函數(shù)中,tmpNames如果換成是IList<CategoryInfo>,即它添加的元素與cInfos是一樣的時(shí),如果要移除其中的一項(xiàng),則cInfos中會同時(shí)移除一項(xiàng)。因?yàn)镃ategoryInfo是類,是引用類型的,而非值類型。所以tmpNames采用了string類型,以避免這個(gè)問題。
對上面這個(gè)函數(shù)直接調(diào)用還稍嫌麻煩,上層程序還需要建立一個(gè)IList<CategoryInfo>對象,因此可以增加一個(gè)函數(shù)來調(diào)用上面的函數(shù)。這樣,上層程序只需要提供分類名,以及是否包含本級分類兩個(gè)參數(shù)就可以了。
//獲取子分類,其中布爾參數(shù)表示是否包含本級分類
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));//根據(jù)名字獲取分類,這個(gè)很簡單,本文略。
}
return cInfos;
}
注意:采用這種方式時(shí),WEB服務(wù)器獲取子分類時(shí)要在數(shù)據(jù)庫服務(wù)器之間有多次往返,降低了性能。采用存儲過程實(shí)現(xiàn)遞歸邏輯,直接返回子分類列表的方式應(yīng)該有更好的性能,尤其是Web服務(wù)器與數(shù)據(jù)庫服務(wù)器不位于同一臺服務(wù)器上時(shí),更會受網(wǎng)絡(luò)影響。
相關(guān)文章
IIS實(shí)現(xiàn)反向代理時(shí)Cookie域的設(shè)置方法
這篇文章主要給大家介紹了關(guān)于IIS實(shí)現(xiàn)反向代理時(shí)Cookie域的設(shè)置方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04一步步打造簡單的MVC電商網(wǎng)站BooksStore(2)
這篇文章主要和大家一起一步步打造一個(gè)簡單的MVC電商網(wǎng)站,MVC電商網(wǎng)站BooksStore第二篇,添加分類導(dǎo)航、加入購物車,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04DataGrid 動態(tài)添加模板列 實(shí)現(xiàn)代碼
模版控件能讓用戶幾乎不用花費(fèi)任何時(shí)間就創(chuàng)建出復(fù)雜的用戶界面. Asp.net有很多控件都使用了模版技術(shù)(DataGrid就是一個(gè)例子). 而這些控件都工作得很好, 通常, 模版可以被保存為ascx文件以增加復(fù)用性. 很有可能, 事前你是不知道你的控件是怎么布局的, 而且你需要動態(tài)的添加一些模版以應(yīng)付不同的事件.2009-04-04.net Core連接MongoDB數(shù)據(jù)庫的步驟詳解
這篇文章主要給大家介紹了關(guān)于.net Core連接MongoDB數(shù)據(jù)庫步驟的相關(guān)資料,文中將實(shí)現(xiàn)的步驟一步步介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02ASP.NET MVC運(yùn)行出現(xiàn)Uncaught TypeError: Cannot set property __MVC
同一相站點(diǎn),有些頁面的客戶端驗(yàn)證能工作,而有些死活不行。打開頁面就出現(xiàn)Uncaught TypeError: Cannot set property __MVC_FormValidation of null錯誤2010-04-04ASP.NET Core自定義本地化教程之從文本文件讀取本地化字符串
使用 ASP.NET Core 創(chuàng)建多語言網(wǎng)站,可讓網(wǎng)站擁有更多受眾。下面這篇文章主要給大家介紹了關(guān)于ASP.NET Core自定義本地化教程之從文本文件讀取本地化字符串的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-09-09Asp.Net的FileUpload類實(shí)現(xiàn)上傳文件實(shí)例
這篇文章主要介紹了Asp.Net的FileUpload類實(shí)現(xiàn)上傳文件的方法,以實(shí)例形式講述了上傳文件類的具體實(shí)現(xiàn)方法,是非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11.net 運(yùn)用二進(jìn)制位運(yùn)算進(jìn)行數(shù)據(jù)庫權(quán)限管理
.net 運(yùn)用二進(jìn)制位運(yùn)算進(jìn)行數(shù)據(jù)庫權(quán)限管理 ,需要的朋友可以參考一下2013-02-02