C# 無限級分類的實現

來源:互聯網
上載者:User

資料庫表: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伺服器與資料庫伺服器不位於同一台伺服器上時,更會受網路影響。
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.