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

推荐订阅源

P
Proofpoint News Feed
博客园 - 聂微东
Application and Cybersecurity Blog
Application and Cybersecurity Blog
MyScale Blog
MyScale Blog
罗磊的独立博客
H
Help Net Security
L
LangChain Blog
T
Threat Research - Cisco Blogs
量子位
S
Securelist
Last Week in AI
Last Week in AI
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
The Hacker News
The Hacker News
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Blog of Author Tim Ferriss
T
Threatpost
Security Latest
Security Latest
P
Palo Alto Networks Blog
Microsoft Security Blog
Microsoft Security Blog
NISL@THU
NISL@THU
F
Full Disclosure
WordPress大学
WordPress大学
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Heimdal Security Blog
J
Java Code Geeks
Recorded Future
Recorded Future
Hugging Face - Blog
Hugging Face - Blog
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
B
Blog RSS Feed
月光博客
月光博客
C
Cisco Blogs
V
Visual Studio Blog
D
DataBreaches.Net
H
Hacker News: Front Page
博客园 - 叶小钗
N
News and Events Feed by Topic
爱范儿
爱范儿
A
Arctic Wolf

博客园 - 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保持向子节点的引用。递归遍历通常有两种方法,一种是在外面,一种是把他写到节点对象自身。终之对树形结构的遍历是很有趣的。
入门学习,希望大家指正。