DBTree

Nov 17, 2007

1 min read

.NET1.0

.NET1.1

.NET2.0

Win2K

WinXP

Win2003

Vista

SQL-Server-2005

VS2005

C#2.0

C#

Windows

.NET

SQL-Server

Visual-Studio

DBA

Dev

Intermediate

Author picture

by MH2538

Contributor

19k Views
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)