惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
I
InfoQ
雷峰网
雷峰网
Hugging Face - Blog
Hugging Face - Blog
IT之家
IT之家
云风的 BLOG
云风的 BLOG
有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
The GitHub Blog
The GitHub Blog
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
G
Google Developers Blog
WordPress大学
WordPress大学
B
Blog
人人都是产品经理
人人都是产品经理
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
量子位
Apple Machine Learning Research
Apple Machine Learning Research
N
Netflix TechBlog - Medium
Last Week in AI
Last Week in AI
博客园 - 聂微东
Jina AI
Jina AI

博客园 - 偶然微笑

asp.net 登陆 asp.net获取URL和IP地址 ASP.NET获取IP与MAC地址的方法 ASP.NET获取IP的6种方法 - 偶然微笑 - 博客园 MS SQL Server 2005 通用分页存储过程 又快又简单的sql2005分页存储过程 SQL SERVER 2005分页存储过程 C#区别和认识四个判等函数 C#中数字日期转中文日期 C#算法(一)选择排序 创建基于ASP.NET的SMTP邮件服务 url传递中文的解决方案总结 C#如何取硬件标志 使用HttpWebRequest提交ASP.NET表单并保持Session和Cookie 提取HTML代码中文字的C#函数 Asp.Net输出数据到EXCEL中 把文字变成图片的小程序 ASP.NET URL Rewrite. URL重写 asp.net采集函数(采集、分析、替换、入库)
.net 无限级分类
偶然微笑 · 2008-09-19 · via 博客园 - 偶然微笑

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page 
{

    System.Text.StringBuilder sb 

= new StringBuilder();
    
//默认深度1;
    public int i = 1;protected void Page_Load(object sender, EventArgs e)
    {
        
if(!this.IsPostBack)
        {
            
this.buildTree();
        }
    }
/// <summary>
    
/// 创建树
    
/// </summary>
    private void buildTree()
    {

        DataSet ds 

= ExecuteDataSet("select * from Sort");//建立关系
        ds.Relations.Add("SortRelation", ds.Tables[0].Columns["SortId"], ds.Tables[0].Columns["ParentSortId"]);//遍历每行并根据数据行关系生成树
        foreach (DataRow dbRow in ds.Tables[0].Rows)
        {
            
if (dbRow.IsNull("ParentSortId"))
            {
                
string s = CreateNode(dbRow["SortName"].ToString());
                sb.Append(s);
                PopulateSubTree(dbRow,i);
                
            }
        }
        
        
this.Response.Write(sb);
    }
/// <summary>
   
/// 遍历子类
   
/// </summary>
   
/// <param name="dbRow">遍历的Row</param>
   
/// <param name="depth">深度(用于显示空格个数)</param>
    private void PopulateSubTree(DataRow dbRow,int depth)
    {
        
++depth;
        
foreach (DataRow childRow in dbRow.GetChildRows("SortRelation"))
        {
            
string s = "";
            
for (int j=1;j<=depth;j++ )
            {
                s 
= "&nbsp" + s;
            }
            s 
+= "|-" + childRow["SortName"].ToString() + "<br>";
           sb.Append(s);
           PopulateSubTree(childRow,depth);
        }
    }
/// <summary>
    
/// 显示父类名称
    
/// </summary>
    
/// <param name="sortname">类别名称</param>
    
/// <returns></returns>
    private string CreateNode(string sortname)
    {
        
string s = "|-"+sortname+"<br>";return s;
     }
#region  返回dataset对象
     
/// <summary>
     
/// 返回dataset对象
     
/// </summary>
     
/// <param name="sql">sql语句</param>
     
/// <returns></returns>
     public DataSet ExecuteDataSet(string sql)
     {
         SqlConnection con 
= new SqlConnection("server=.;database=sort;uid=sa;pwd=;");
         DataSet ds 
= new DataSet();
         SqlDataAdapter sda 
= new SqlDataAdapter(sql, con);
         sda.Fill(ds);
         
return ds;
     }
     
#endregion

}