zoukankan      html  css  js  c++  java
  • 树形结构在开发中的应用

    树形结构在开发中的应用
    李洪根
    CREATE TABLE [dbo].[tbTree] (
    [ID] [int] IDENTITY (1, 1) NOT NULL ,
    [Context] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    [ParentID] [int] NULL ,
    [depth] [int] NULL
    ) ON [PRIMARY]
    insert tbtree (ID,Context,ParentID,depth)  values ( 1,'中国',0,0)
    insert tbtree (ID,Context,ParentID,depth) values ( 2,'北京',1,1)
    insert tbtree (ID,Context,ParentID,depth) values ( 3,'天津',1,1)
    insert tbtree (ID,Context,ParentID,depth) values ( 4,'河北省',1,1)
    insert tbtree (ID,Context,ParentID,depth) values ( 5,'广东省',1,1)
    insert tbtree (ID,Context,ParentID,depth) values ( 6,'广州',5,2)
    insert tbtree (ID,Context,ParentID,depth) values ( 7,'四川省',1,1)
    insert tbtree (ID,Context,ParentID,depth) values ( 8,'成都',7,2)
    insert tbtree (ID,Context,ParentID,depth) values ( 9,'深圳',5,2)
    insert tbtree (ID,Context,ParentID,depth) values ( 10,'石家庄',4,2)
    insert tbtree (ID,Context,ParentID,depth) values ( 11,'辽宁省',1,1)
    insert tbtree (ID,Context,ParentID,depth) values ( 12,'大连',11,2)
    insert tbtree (ID,Context,ParentID,depth) values ( 13,'上海',1,1)
    insert tbtree (ID,Context,ParentID,depth) values ( 14,'天河软件园',6,3)
    insert tbtree (ID,Context,ParentID,depth) values ( 15,'汕头',5,2)
    Dim CN As ADODB.Connection                '定义数据库的连接
    Dim Rs As ADODB.Recordset

    '工程--->引用--->Microsoft ActiveX Data Object 2.x(版本号)
    Private Sub Form_Load()
    Set CN = New ADODB.Connection
    ‘连接数据库
    CN.ConnectionString =
    "Provider=sqloledb;Data Source=pmserver;Initial Catalog=Benchmark;User Id=sa;Password=sa;"
    CN.Open
    End Sub

    Private Sub AddTree(ByVal intDepth As Integer)
    ‘打开记录集,得到深度小于些深度的所有节点,并按深度排序
    Set Rs = New ADODB.Recordset
    Rs.Open "select * from tbTree
    where depth<='" & intDepth & "'
    order by depth", CN, adOpenDynamic, adLockReadOnly
    Dim Xnod As Node
    Do While Not Rs.EOF
    If Rs.Fields("depth") = 0 Then
    ‘加入根结点
    Set Xnod = TreeView1.Nodes.Add(, , "key" & Rs.Fields("id"), Rs.Fields("context"))
    Else
    ‘加入子节点
    Set Xnod = TreeView1.Nodes.Add
    ("key" & Rs.Fields("parentid"), tvwChild, "key" & Rs.Fields("id"), Rs.Fields("context"))
    End If
    Xnod.EnsureVisible
    Rs.MoveNext
    Loop
    Rs.Close
    End Sub
    CREATE PROCEDURE  spGetTree (
    @ID int)
    as
    set nocount on
    declare @tmp table (Id int,ConText varchar(50),ParentID int,depth int)
    insert @tmp select * from tbtree where ID=@ID
    while exists
    (select 1 from tbtree a,@tmp b where a.ParentID=b.ID and a.ID not in (select ID from @tmp))
    insert @tmp
    select a.* from tbtree a,@tmp b where a.ParentID=b.ID and a.ID not in (select ID from @tmp)
    select * from @tmp
    set nocount off
    GO
    Private Sub AddTreeEx(ByVal intID As Integer)
    Set Rs = New ADODB.Recordset
    Rs.Open "spGettree " & intID, CN, adOpenDynamic, adLockReadOnly
    Dim Xnod As Node
    Do While Not Rs.EOF
    If Rs.Fields("parentID") = 0 Then
    Set Xnod = TreeView1.Nodes.Add(, , "key" & Rs.Fields("id"), Rs.Fields("context"))
    Else
    Set Xnod = TreeView1.Nodes.Add
    ("key" & Rs.Fields("parentid"), tvwChild,
    "key" & Rs.Fields("id"), Rs.Fields("context"))
    End If
    Xnod.EnsureVisible
    Rs.MoveNext
    Loop
    Rs.Close
    End Sub
        Private ds As New DataSet ()
    ' AddTree递归函数每次都要用到数据集中的一个表,所以定义成private
    Private Sub Form1_Load
    (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' '定义数据库连接
    Dim CN As New SqlConnection()
    Try
    '初始化连接字符串
    CN.ConnectionString =
    "data source=pmserver;initial catalog=
    Benchmark;persist security info=False;user id=sa;Password=sa;"
    CN.Open()
    '添加命令,从数据库中得到数据
    Dim sqlCmd As New SqlCommand()
    sqlCmd.Connection = CN
    sqlCmd.CommandText = "select * from tbtree"
    sqlCmd.CommandType = CommandType.Text
    Dim adp As SqlDataAdapter = New SqlDataAdapter(sqlCmd)
    adp.Fill(ds)
    Catch ex As Exception
    MsgBox(ex.Message)
    Finally
    '关闭连接
    CN.Close()
    End Try
    '调用递归函数,完成树形结构的生成
    AddTree(0, Nothing)
    End Sub

    '̀递归添加树的节点
    Private Sub AddTree(ByVal ParentID As Integer, ByVal pNode As TreeNode)
    Dim Node As TreeNode
    Dim dvTree As New DataView()
    dvTree = New DataView(ds.Tables(0))
    '过滤ParentID,得到当前的所有子节点
    dvTree.RowFilter = "PARENTID = " + ParentID.ToString

    Dim Row As DataRowView
    For Each Row In dvTree
    If pNode Is Nothing Then '判断是否根节点
    '̀添加根节点
    Node = TreeView1.Nodes.Add(Row("context").ToString())
    '̀再次递归
    AddTree(Int32.Parse(Row("ID").ToString()), Node)
    Else
    ‘添加当前节点的子节点
    Node = pNode.Nodes.Add(Row("context").ToString())
    '̀再次递归
    AddTree(Int32.Parse(Row("ID").ToString()), Node)
    End If
    Node.EnsureVisible()
    Next
    End Sub
    		 DataSet ds=new DataSet();
    private void Form1_Load(object sender, System.EventArgs e)
    {
    // 定义数据库连接
    SqlConnection CN = new SqlConnection();
    try
    {
    //初始化连接字符串
    CN.ConnectionString=
    "data source=pmserver;initial catalog=Benchmark;
    persist security info=False;user id=sa;Password=sa;";
    CN.Open();
    //添加命令,从数据库中得到数据
    SqlCommand sqlCmd= new SqlCommand();
    sqlCmd.Connection = CN;
    sqlCmd.CommandText = "select * from tbTree";
    sqlCmd.CommandType = CommandType.Text ;
    SqlDataAdapter adp = new SqlDataAdapter(sqlCmd);
    adp.Fill(ds);
    }
    catch (Exception ex)
    {
    throw (ex);
    }
    finally
    {
    CN.Close();
    }
    //调用递归函数,完成树形结构的生成
    AddTree(0, (TreeNode)null);
    }

    // 递归添加树的节点
    public void AddTree(int ParentID,TreeNode pNode)
    {
    DataView dvTree = new DataView(ds.Tables[0]);
    //过滤ParentID,得到当前的所有子节点
    dvTree.RowFilter = "[PARENTID] = " + ParentID;
    foreach(DataRowView Row in dvTree)
    {
    if(pNode == null)
    { //'̀添加根节点
    TreeNode Node = treeView1.Nodes.Add(Row["ConText"].ToString());
    AddTree(Int32.Parse(Row["ID"].ToString()),Node); //再次递归
    }
    else
    { //添加当前节点的子节点
    TreeNode Node = pNode.Nodes.Add(Row["ConText"].ToString());
    AddTree(Int32.Parse(Row["ID"].ToString()),Node); //再次递归
    }
    }
    }
  • 相关阅读:
    做好测试计划和测试用例的工作的关键是什么?
    windows创建虚拟环境
    面试题整理
    【爬虫】使用selenium设置cookie
    【爬虫】随机获取UA
    Scrapy爬虫提高效率
    【爬虫】多线程爬取糗事百科写入文件
    【爬虫】多线程爬取表情包
    【爬虫】Condition版的生产者和消费者模式
    【爬虫】Load版的生产者和消费者模式
  • 原文地址:https://www.cnblogs.com/fuyingke/p/429201.html
Copyright © 2011-2022 走看看