65.9K
CodeProject is changing. Read more.
Home

DBTree

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.43/5 (9 votes)

Nov 17, 2007

CPOL
viewsIcon

19050

downloadIcon

288

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()".