国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#遞歸方法實現無限級分類顯示效果

2024-09-07 17:05:38
字體:
來源:轉載
供稿:網友

這篇文章主要講述C#遞歸方法實現無限級分類顯示效果,結合完整實例形式分析了C#遞歸算法與數據元素遍歷的相關技巧,具有一定參考借鑒價值,需要的朋友不妨看看。

數據庫表: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 GetChildCategories(IList 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 tmpNames = new List(); //臨時存儲獲取的子
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,即它添加的元素與cInfos是一樣的時,如果要移除其中的一項,則cInfos中會同時移除一項。因為CategoryInfo是類,是引用類型的,而非值類型。所以tmpNames采用了string類型,以避免這個問題。
對上面這個函數直接調用還稍嫌麻煩,上層程序還需要建立一個IList對象,因此可以增加一個函數來調用上面的函數。這樣,上層程序只需要提供分類名,以及是否包含本級分類兩個參數就可以了。
//獲取子分類,其中布爾參數表示是否包含本級分類
public IList GetCategories( string cName, bool IsIncludeSelf)
{
IList cInfos = new List();
cInfos = GetChildCategories(cInfos, cName);
if (IsIncludeSelf == true)
{
cInfos.Insert(0, GetByName(cName));//根據名字獲取分類,這個很簡單,本文略。
}
return cInfos;
}
注意:采用這種方式時,WEB服務器獲取子分類時要在數據庫服務器之間有多次往返,降低了性能。采用存儲過程實現遞歸邏輯,直接返回子分類列表的方式應該有更好的性能,尤其是Web服務器與數據庫服務器不位于同一臺服務器上時,更會受網絡影響。

以上就是關于C#遞歸方法實現無限級分類顯示效果的全部內容,更多相關內容,請繼續關注錯新技術頻道。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新民市| 桓仁| 桦川县| 黑山县| 昌江| 长宁县| 玛纳斯县| 临颍县| 新津县| 永泰县| 浦城县| 当阳市| 黄冈市| 安达市| 甘泉县| 原阳县| 新邵县| 旌德县| 三都| 麻栗坡县| 许昌县| 元谋县| 七台河市| 遵义县| 辽宁省| 新河县| 屏南县| 阳城县| 康平县| 巴南区| 格尔木市| 武山县| 庆安县| 绥德县| 昌邑市| 关岭| 中超| 容城县| 息烽县| 息烽县| 兴山县|