Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

DBTree

0.00/5 (No votes)
16 Nov 2007 1  
A DB Base Tree that load it's nodes from a table
Screenshot - LoadTree.jpg

Introduction

This is a simple code to show how we can load a tree nodes from a table records.

How we use this code

At first step we must create a DataBase and name it "dbTree".
Then we must run this script to create table that reserve our tree nodes. We name this table "tPOI":

//

// 

USE [dbTree]
GO
/****** Object:  Table [dbo].[tPOI]    Script Date: 11/17/2007 09:32:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tPOI](
 [NodeID] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
 [ParentID] [numeric](18, 0) NULL,
 [NodeName] [nchar](10) COLLATE Arabic_CI_AS NULL,
 [InUse]  NULL CONSTRAINT [DF_tPOI_InUse]  DEFAULT ((1)),
CONSTRAINT [PK_tPOI] PRIMARY KEY CLUSTERED 
(
 [NodeID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
//

And finally load this records to "tPOI".

//

NodeID  ParentID          NodeName                InUse
1         0             RootNode1        True
2         1             Child1            True
3         1             Child2            True
4         1             Child3            True
5         2             Child1_1        True
6         2             Child1_2        True
7         0             RootNode2        True
//

How it works?

Our strategy to load nodes is simple: RootNodes don't have ParentNode so we set their ParentNode Field to Zero ("0") and every child node has a parent node that help us to attach this node to its parent.
Is Simple, isn't?

Now we go to code section:
There is just one function that load records to tree, "treeLoader()".

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here