天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

C# 無限級分類的實現(xiàn)

數(shù)據(jù)庫表:CategoryInfo
字段名 類型
ciID int //記錄序號,自增量
ciName nvarchar(20) //分類名
ciParent int //父分類序號
ciLayer int //所處的層次
ciDescription nvarchar(200) //對分類的描述
分類的類設(shè)計
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>(); //臨時存儲獲取的子
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是一樣的時,如果要移除其中的一項,則cInfos中會同時移除一項。因為CategoryInfo是類,是引用類型的,而非值類型。所以tmpNames采用了string類型,以避免這個問題。
對上面這個函數(shù)直接調(diào)用還稍嫌麻煩,上層程序還需要建立一個IList<CategoryInfo>對象,因此可以增加一個函數(shù)來調(diào)用上面的函數(shù)。這樣,上層程序只需要提供分類名,以及是否包含本級分類兩個參數(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ù)名字獲取分類,這個很簡單,本文略。
}
return cInfos;
}
注意:采用這種方式時,WEB服務(wù)器獲取子分類時要在數(shù)據(jù)庫服務(wù)器之間有多次往返,降低了性能。采用存儲過程實現(xiàn)遞歸邏輯,直接返回子分類列表的方式應(yīng)該有更好的性能,尤其是Web服務(wù)器與數(shù)據(jù)庫服務(wù)器不位于同一臺服務(wù)器上時,更會受網(wǎng)絡(luò)影響。

AspNet技術(shù)C# 無限級分類的實現(xiàn),轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 亚洲不卡高清免v无码屋 | www在线小视频免费 www伊人网 | seyeye在清在线 | 5g在线视讯年龄确认海外禁止进入 | 国产成人拍精品视频网 | 国产成人一区免费观看 | 麻花传媒XK在线观看 | 亚洲欧美人成视频在线 | 97国内精品久久久久久久影视 | 亚洲国产日韩a精品乱码 | 18岁末年禁止观看免费1000个 | 无码中文字幕热热久久 | 亚洲AV无码乱码国产麻豆穿越 | 成年女人免费播放影院 | 国产成人免费观看 | 日日撸影院在线 | 强奸日本美女小游戏 | 欧美一区二区高清 | 四虎永久在线精品国产免费 | 老外的好大c的我好爽 | 亚洲国产精品久久精品成人网站 | 美女裸露胸部100%无遮挡 | 一本之道高清在线3线观看 一本之道高清视频在线观看 | 麻豆Av国产在线播放 | 午夜福利影院私人爽爽 | 免费国产久久啪在线 | 麻豆国产原创中文AV网站 | 久草在线福利资站免费视频 | 竹菊影视一区二区三区 | 99精品福利视频 | 亚洲天堂2017无码 | 亚洲欧洲日韩天堂无吗 | 欧美色图天堂网 | 日本69xxxx| 欲香欲色天天影视大全 | 丝袜美女自摸 | 亚洲乱色视频在线观看 | 第九色区av天堂 | 中文字幕在线观看亚洲 | 亚洲色大成网站www久久九九 | 久草在在线免视频在线观看 |