Click here to Skip to main content
15,885,546 members
Articles / Database Development / SQL Server

Hierarchical Tree Represented by Modified Preorder Tree Traversal Technique using C# 3.0 and SQL 2005

Rate me:
Please Sign up or sign in to vote.
4.93/5 (29 votes)
10 Dec 2008CPOL6 min read 112.8K   3.3K   86  
Gathering of various algorithms into one library to transform Hierarchical trees between various formats, and allows them to be represented into SQL 2005, the formats supported are TreeView, Textual, Tabular, Modified Preorder Tree Traversal and Graphical.
using System;
using System.Data;
using System.Configuration;
using System.Data.Common;
using System.Collections.Generic;
using System.Data.SqlClient;
using MPTT.BLL;

namespace MPTT.DAL
{
    public abstract class DataAccess
    {
        public static Boolean useStaticSingleConection = false;
        private static SqlConnection conStatic = null;
        protected SqlConnection con;
        protected Boolean endUpdate;

        private string _connectionString = "";
        public string ConnectionString
        {
            get { return _connectionString; }
            set { _connectionString = value; }
        }

        public DataAccess()
        {
            this.ConnectionString = "";
            Init();
        }

        public DataAccess(string conString)
        {
            this.ConnectionString = conString;
            Init();
        }

        protected virtual void Init()
        {
            endUpdate = false;
        }

        public virtual void BegingUpdate()
        {
            try
            {
                if (conStatic == null || conStatic.State!=ConnectionState.Open)
                {
                    con = new SqlConnection(ConnectionString);
                    con.Open();
                    conStatic = con;
                }
                con = conStatic;
            }
            catch
            {
                con = null;
            }
        }

        public virtual void EndUpdate()
        {
            if (con != null)
            {
                if(useStaticSingleConection)
                    con = null;
                else
                {
                    //remove unmanaged resources of our static connection by
                    //calling dispose method after closing the connection
                    con.Close();
                    con.Dispose();
                    conStatic = null;
                    con = null;
                }
            }
        }

        protected virtual Boolean InternalBeginUpdate()
        {
            //Support multiple call to this function
            //by checking con at first
            if (con != null)
                return true;
            //
            BegingUpdate();
            endUpdate = (con != null);
            return (con != null);
        }

        protected virtual void InternalEndUpdate()
        {
            if (endUpdate)
                EndUpdate();
        }



        protected int ExecuteNonQuery(DbCommand cmd)
        {
            return cmd.ExecuteNonQuery();
        }

        protected IDataReader ExecuteReader(DbCommand cmd)
        {
            return ExecuteReader(cmd, CommandBehavior.Default);
        }

        protected IDataReader ExecuteReader(DbCommand cmd, CommandBehavior behavior)
        {
            return cmd.ExecuteReader(behavior);
        }

        protected object ExecuteScalar(DbCommand cmd)
        {
            return cmd.ExecuteScalar();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Government
Qatar Qatar
Programmer since 1990 with Pascal, VC++, C#, ASP.NET, jQuery, J2EE and Android.
PMP Certified since 2009.
PSP Certified since 2005.
Business & System analyst since 2004.
Led teams in between 8 to 30 members.
Worked for www.beinsports.net, www.harf.com, www.islamweb.net, islam.gov.qa, islamonline.net.

Comments and Discussions