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

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed
Security Latest
Security Latest
P
Privacy International News Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AI
AI
Cisco Talos Blog
Cisco Talos Blog
K
Kaspersky official blog
S
Secure Thoughts
PCI Perspectives
PCI Perspectives
Simon Willison's Weblog
Simon Willison's Weblog
D
DataBreaches.Net
GbyAI
GbyAI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
罗磊的独立博客
V
Visual Studio Blog
aimingoo的专栏
aimingoo的专栏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
V
V2EX
Last Week in AI
Last Week in AI
有赞技术团队
有赞技术团队
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tenable Blog
T
Threat Research - Cisco Blogs
T
Troy Hunt's Blog
V2EX - 技术
V2EX - 技术
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
L
Lohrmann on Cybersecurity
F
Full Disclosure
H
Help Net Security
博客园 - Franky
Stack Overflow Blog
Stack Overflow Blog
N
Netflix TechBlog - Medium
Engineering at Meta
Engineering at Meta
A
Arctic Wolf
O
OpenAI News
S
Securelist

博客园 - goodbaby

辞职了 爱上无聊 读程序有感 把网通的网关接口程序重写了 深深陷入困境 我的技术和观点 我的AOP初步 静态和动态控件回递数据的处理差别 membership and roleship 我的asp.net 2.0初体验 一点技术,一点生活 简单体验多层应用 基于服务的架构 变化,感触 又可以进自己的blog了 浅谈asp.net UI 浅谈验证码 浅谈基于角色的安全 简单的URL重写
浅谈数据库里的自引用
goodbaby · 2004-11-19 · via 博客园 - goodbaby

在Northiwind里,有一张雇员表,雇员关系为层次化的,所以要用树来显示:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace WebApplication1
{
    
/// <summary>
    
/// WebForm5 的摘要说明。
    
/// </summary>

    public class WebForm5 : System.Web.UI.Page
    
{
        
protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
        Microsoft.Web.UI.WebControls.TreeNode rootnode;
      
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            SqlConnection con=new SqlConnection("server=GOODBABY;uid=sa;password=goodbaby;database=Northwind");
            DataSet myDS
=new DataSet();
            SqlDataAdapter mySqlDAP
=new SqlDataAdapter("select EmployeeID,ReportsTo,LastName from Employees",con);
            
try
            
{
                con.Open();
                mySqlDAP.Fill(myDS,
"Employees");
                DataTable myDataTable
=myDS.Tables["Employees"];
                myDS.Relations.Add(
"SelfReferencing",myDataTable.Columns["EmployeeID"],myDataTable.Columns["ReportsTo"],false);
                
foreach(DataRow Item in myDataTable.Rows)
                
{
                    
if(Item.IsNull("ReportsTo"))
                    
{
                        Microsoft.Web.UI.WebControls.TreeNode treenode
=new Microsoft.Web.UI.WebControls.TreeNode();
                        treenode.Text
=(string)Item["LastName"];
                        rootnode
=treenode;
                        TreeView1.Nodes.Add(treenode);
                        ForTree(treenode,Item);
                        
break;
                    }

                }

            }

            
catch(Exception ex)
            
{
                
throw ex;
            }

        }



        
private void ForTree( Microsoft.Web.UI.WebControls.TreeNode node,DataRow row)
        
{
            
if(!row.IsNull("ReportsTo"))
            
{
                Microsoft.Web.UI.WebControls.TreeNode tempNode
=new Microsoft.Web.UI.WebControls.TreeNode();
                tempNode.Text
=(string)row["LastName"];
                node.Nodes.Add(tempNode);
                
foreach(DataRow Item in row.GetChildRows("SelfReferencing"))
                
{
                    ForTree(tempNode,Item);
                }

            }

            
else
            
{
                Microsoft.Web.UI.WebControls.TreeNode tempNode;
                tempNode
=node;
                
foreach(DataRow Item in row.GetChildRows("SelfReferencing"))
                
{
                    ForTree(tempNode,Item);
                }

            }

            
        }


        
Web 窗体设计器生成的代码
    }

}

效果:

这实际上是一棵树,这里用深度递归遍历他,也就是说如果你能构建一棵树,那么几可以从根节点遍历他,更通用的一种模式是声明一个节点的接口,在他下面派生子类,那么象有子节点的节点就可以这样MyInterfaceForBase[] myInterfaceforBase保持向子节点的引用。递归遍历通常有两种方法,一种是在外面,一种是把他写到节点对象自身。终之对树形结构的遍历是很有趣的。
入门学习,希望大家指正。