麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > C# > 正文

C# 無限級分類的實現

2024-09-07 17:05:16
字體:
來源:轉載
供稿:網友
數據庫表: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類型,以避免這個問題。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 成人做爰s片免费看网站 | 国产噜噜噜 | 男女羞羞视频在线免费观看 | 欧美日韩精品中文字幕 | 成人不卡一区二区 | 毛片视频免费观看 | 激情久久一区二区 | 久久久久久久久久久久久久国产 | 干少妇av | 在线观看免费视频麻豆 | 深夜激情视频 | 草久视频在线观看 | av在线看网站 | 久久国产中文字幕 | 成人9禁啪啪无遮挡免费 | 日本网站一区 | 一本到免费视频 | 日韩视频www | 国产亚洲高清在线精品不卡 | 热99在线视频| 亚洲国产女同久久 | 久久蜜桃香蕉精品一区二区三区 | 9191色| 毛片视 | 毛片免费在线播放 | 亚洲电影在线观看高清免费 | 91久久国产综合久久91精品网站 | 二级大黄大片高清在线视频 | 免费在线观看毛片视频 | 99在线精品视频免费观看20 | 久久精品久 | 日韩欧美中文字幕视频 | 久久91亚洲精品久久91综合 | 久久爽久久爽久久av东京爽 | 日韩视频一区二区三区四区 | 蜜桃视频在线播放 | 国产99久久 | 久久爽精品区穿丝袜 | 国产大片在线观看 | 天堂成人国产精品一区 | 91网视频|