递归实现无限级分类显示

string html = "";
protected string GetClass(int ParentID,string TableName,string FileName)
{
SqlParameter sparm
= new SqlParameter("@ParentID", SqlDbType.Int);
sparm.Value
= ParentID;
SqlDataReader sdr
= SqlHelper.ExecuteReader(SqlHelper.ConnectionString, CommandType.Text, "SELECT * FROM " + TableName + " WHERE ParentID = @ParentID ORDER BY ClassID ASC",sparm);
if (sdr.HasRows)
{
html
+= "<ul>\n";
}
while (sdr.Read())
{
html
+= "<li><a href=\""+FileName+"?ClassID=" + sdr[0].ToString() + "\">" + sdr["ClassName"].ToString() + "</a>\n";
if (Convert.ToInt16(sdr["Child"]) > 0)
{
GetClass(Convert.ToInt16(sdr[
"ClassID"]),TableName,FileName); //如果有子分类则递归调用
}
html
+= "</li>\n";
}
if (sdr.HasRows)
{
html
+= "</ul>\n";
}
return html;
}

原文地址:https://www.cnblogs.com/superfeeling/p/2129996.html