OOP Question DB Access Wrapper Classes

This is a discussion on OOP Question DB Access Wrapper Classes within the C# Programming forums, part of the General Programming Boards category; Hello All, I need some expert opinion on a set of Database Access Wrapper Classes which I am trying to ...

  1. #1
    Registered User digioz's Avatar
    Join Date
    Oct 2005
    Location
    Chicago, IL
    Posts
    3

    OOP Question DB Access Wrapper Classes

    Hello All,

    I need some expert opinion on a set of Database Access Wrapper Classes
    which I am trying to funnel down to a single class.

    Here is what I have:

    clsData: The entry point
    clsMSSQL: Microsoft SQL DB Access Wrapper
    clsMySQL: MySQL DB Access Wrapper

    The idea here was that the programmer can initiate a single object and depending
    on the database type he selects, that single object will instantiate other objects to
    get the DB query done. It would also make it easy to add other types of database
    support later on after the application creation.

    My Question is regarding the clsData (entry point) Class. Is this the best way to code it?

    clsData Class:

    Code:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    namespace digioz
    {
    
    /// <summary>
    /// Database Interface 
    /// </summary>
    public interface IData
    {
        void openConnection();
        void closeConnection();
        //void QueryDBReader(string sql);
        void QueryDBDataset(string sql);
        DataTable getDBDataset(string sql, string _host, string _name, string _user, string _pass);
        void UpdateDBDataset();
        void DeleteDBDataset(int primarykey);
        void ExecDB(string sql);
        object ExecDBScalar(string sql);
    }
    
    /// <summary>
    /// Base Class for Data retrieval from different database types
    /// </summary>
    public class clsData
    {
        #region "Variable Declaration"
            private string m_host;
            private string m_name;
            private string m_user;
            private string m_pass;
            private string m_type;
    
            // Database Variables
            private clsMSSQL coMSSQL;
            private clsMySql coMySQL;
            public DataSet ds;
        #endregion
    
        public clsData()
    	{
    		//
    		// TODO: Add constructor logic here
    		//
    	}
    
        public void openConnection()
        {
            if (m_type == "MSSQL")
            {
                coMSSQL = new clsMSSQL();          
                coMSSQL.dbhost = m_host;
                coMSSQL.dbname = m_name;
                coMSSQL.dbuser = m_user;
                coMSSQL.dbpass = m_pass;
                coMSSQL.openConnection();
            }
            else if (m_type == "MYSQL")
            {
                coMySQL = new clsMySql();
                coMySQL.dbhost = m_host;
                coMySQL.dbname = m_name;
                coMySQL.dbuser = m_user;
                coMySQL.dbpass = m_pass;
                coMySQL.openConnection();
            }
        }
    
        public void closeConnection()
        {
            if (m_type == "MSSQL")
            {
                coMSSQL.closeConnection();
            }
            else if (m_type == "MYSQL")
            {
                coMySQL.closeConnection();
            }
        }
    
        public void QueryDBDataset(string sql)
        {
            if (m_type == "MSSQL")
            {
                coMSSQL.QueryDBDataset(sql);
                ds = coMSSQL.ds;
            }
            else if (m_type == "MYSQL")
            {
                coMySQL.QueryDBDataset(sql);
                ds = coMySQL.ds;
            }
        }
    
        public object ExecDBScalar(string sql)
        {
            object loReturn = null;
    
            if (m_type == "MSSQL")
            {
                loReturn = coMSSQL.ExecDBScalar(sql);
            }
            else if (m_type == "MYSQL")
            {
                loReturn = coMySQL.ExecDBScalar(sql);
            }
            return loReturn;
        }
    
        public void ExecDB(string sql)
        {
            if (m_type == "MSSQL")
            {
                coMSSQL.ExecDB(sql);
            }
            else if (m_type == "MYSQL")
            {
                coMSSQL.ExecDB(sql);
            }
        }
    
        #region "Public Properties"
            public string dbhost
            {
                get { return m_host; }
                set { m_host = value; }
            }
    
            public string dbname
            {
                get { return m_name; }
                set { m_name = value; }
            }
    
            public string dbuser
            {
                get { return m_user; }
                set { m_user = value; }
            }
    
            public string dbpass
            {
                get { return m_pass; }
                set { m_pass = value; }
            }
    
            public string dbtype
            {
                get { return m_type; }
                set { m_type = value; }
            }
        #endregion
    }
    }
    clsMSSQL Class:

    Code:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    
    namespace digioz
    {
    
    /// <summary>
    /// MSSQL Database Communication Class
    /// </summary>
    public class clsMSSQL : IData
    {
        #region "Variable Declaration"
            private string m_host;
            private string m_name;
            private string m_user;
            private string m_pass;
    
            public DataSet ds;
            public System.Data.SqlClient.SqlDataAdapter da;
            public DataTable dt = new DataTable();
            public System.Data.SqlClient.SqlCommandBuilder cd;
            public SqlCommandBuilder cb;
            public SqlConnection cn;
            public System.Data.SqlClient.SqlDataReader reader;
            public string cnString;
            public string dllversion = "1.0.0";
            public string Err;
            public int ErrNumber;
        #endregion
        
        public clsMSSQL()
        {
            //
            // TODO: Add constructor logic here
            // 
        }
    
        public void openConnection()
        {
            cnString = "Server=" + m_host + ";Database=" + m_name + ";User ID=" + m_user + ";Password=" + m_pass + ";Trusted_Connection=False;";
            cn = new SqlConnection(cnString);
            try
            {
                cn.Open();
                Err = "";
            }
            catch (SqlException ex)
            {
                Err = ex.Message.ToString();
            }
        }
    
        public void closeConnection()
        {
            cn.Close();
        }
    
        //public void QyeryDBReader(string sql)
        //{
        //    reader = null;
        //    SqlCommand cmd = new SqlCommand(sql, conn);
        //    try
        //    {
        //        reader = cmd.ExecuteReader();
        //    }
        //    catch (SqlException ex)
        //    {
        //        err += "Error: " + ex.Message.ToString();
        //        errNumber = ex.Number;
        //    }
        //    finally
        //    {
        //        if (!(reader == null))
        //        {
        //            reader.Close();
        //        }
        //    }
        //    return reader;
        //}
    
        public void QueryDBDataset(string sql)
        {
            try
            {
                SqlCommand cmd = new SqlCommand(sql, cn);
                da = new SqlDataAdapter(cmd);
                ds = new DataSet();
                da.SelectCommand = cmd;
                cb = new SqlCommandBuilder(da);
                da.Fill(ds);
            }
            catch (SqlException ex)
            {
                Err = ex.Message.ToString();
            }
        }
    
        public DataTable getDBDataset(string sql, string _host, string _name, string _user, string _pass)
        {
            m_host = _host;
            m_name = _name;
            m_user = _user;
            m_pass = _pass;
    
            openConnection();
    
            try
            {
                SqlCommand cmd = new SqlCommand(sql, cn);
                da.SelectCommand = cmd;
                cb = new SqlCommandBuilder(da);
                da.Fill(ds);
            }
            catch (SqlException ex)
            {
                Err += "Error: " + ex.Message.ToString();
                ErrNumber = ex.Number;
            }
            closeConnection();
    
            return ds.Tables[0];
        }
    
        public System.Data.SqlClient.SqlDataReader QueryDBReader(string sql)
        {
            reader = null;
    
            SqlCommand cmd = new SqlCommand(sql, cn);
            try
            {
                reader = cmd.ExecuteReader();
            }
            catch (SqlException ex)
            {
                Err = ex.Message.ToString();
            }
            finally
            {
                if ((reader != null))
                    reader.Close();
            }
    
            return reader;
        }
    
        public object ExecDBScalar(string sql)
        {
            SqlCommand cmd = new SqlCommand(sql, cn);
            object oReturn;
    
            try
            {
                oReturn = cmd.ExecuteScalar().ToString();
            }
            catch (SqlException ex)
            {
                Err = ex.Message.ToString();
                ErrNumber = ex.Number;
                oReturn = null;
            }
            return oReturn;
        }
    
        public void ExecDB(string sql)
        {
            try
            {
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                Err = ex.Message.ToString();
            }
        }
    
        public void UpdateDBDataset()
        {
            try
            {
                dt = ds.Tables[0];
                DataTable changes = dt.GetChanges();
    
                da.Update(changes);
                dt.AcceptChanges();
            }
            catch (SqlException ex)
            {
                Err = ex.Message.ToString();
            }
        }
    
        public void DeleteDBDataset(int primarykey)
        {
            try
            {
                ds.Tables[0].Rows[primarykey].Delete();
            }
            catch (SqlException ex)
            {
                Err += "Error: " + ex.Message.ToString();
                ErrNumber = ex.Number;
            }
        }
    
        public void CreateDatabase(string DatabaseName)
        {
            try
            {
                this.ExecDB("CREATE DATABASE " + DatabaseName + ";");
            }
            catch (SqlException ex)
            {
                Err = ex.Message.ToString();
            }
        }
    
        public string GetDLLVersion()
        {
            return "DigiOz MSSQL DLL Version " + dllversion;
        }
    
        #region "Public Properties"
    
        public string dbhost
        {
            get { return m_host; }
            set { m_host = value; }
        }
    
        public string dbname
        {
            get { return m_name; }
            set { m_name = value; }
        }
    
        public string dbuser
        {
            get { return m_user; }
            set { m_user = value; }
        }
    
        public string dbpass
        {
            get { return m_pass; }
            set { m_pass = value; }
        }
    
        #endregion
    
    } 
    
    }
    clsMySQL Class:

    Code:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using MySql.Data.MySqlClient;
    
    namespace digioz
    {
    
    /// <summary>
    /// MySQL Database Communication Class
    /// </summary>
    public class clsMySql : IData 
    {
        #region "Variable Declaration"
                private string pr_dbhost;
                private string pr_dbuser;
                private string pr_dbpass;
                private string pr_dbname;
                private string connStr;
                public MySqlConnection conn;
                public MySqlDataReader reader;
                public DataSet ds = new DataSet();
                public MySqlDataAdapter da = new MySqlDataAdapter();
                public MySqlCommandBuilder cb;
                public DataTable dt = new DataTable();
                public string Err;
                public int ErrNumber;
                public string dllversion = "1.0.0"; 
        #endregion
    
    	public clsMySql()
    	{
    		//
    		// TODO: Add constructor logic here
    		//
    	}
            public void openConnection()
            {
                connStr = string.Format("server={0};user id={1}; password={2}; database={3}; pooling=false; Allow Zero DateTime=False;", dbhost, dbuser, dbpass, dbname);
                if (!(conn == null))
                {
                    conn.Close();
                }
                try
                {
                    conn = new MySqlConnection(connStr);
                    conn.Open();
                }
                catch (MySqlException ex)
                {
                    Err += "Error: " + ex.Message.ToString();
                    ErrNumber = ex.Number;
                }
            }
    
            public void closeConnection()
            {
                try
                {
                    conn.Close();
                }
                catch (MySqlException ex)
                {
                    Err += "Error: " + ex.Message.ToString();
                    ErrNumber = ex.Number;
                }
            }
    
        //public void QueryDBReader(string sql)
        //{
        //    reader = null;
        //    MySqlCommand cmd = new MySqlCommand(sql, conn);
        //    try
        //    {
        //        reader = cmd.ExecuteReader();
        //    }
        //    catch (MySqlException ex)
        //    {
        //        Err += "Error: " + ex.Message.ToString();
        //        errNumber = ex.Number;
        //    }
        //    finally
        //    {
        //        if (!(reader == null))
        //        {
        //            reader.Close();
        //        }
        //    }
        //    return reader;
        //}
    
            public void QueryDBDataset(string sql)
            {
                try
                {
                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    da.SelectCommand = cmd;
                    cb = new MySqlCommandBuilder(da);
                    da.Fill(ds);
                }
                catch (MySqlException ex)
                {
                    Err += "Error: " + ex.Message.ToString();
                    ErrNumber = ex.Number;
                }
            }
    
            public DataTable getDBDataset(string sql, string _host, string _name, string _user, string _pass)
            {
                pr_dbhost = _host;
                pr_dbname = _name;
                pr_dbuser = _user;
                pr_dbpass = _pass;
    
                openConnection();
    
                try
                {
                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    da.SelectCommand = cmd;
                    cb = new MySqlCommandBuilder(da);
                    da.Fill(ds);
                }
                catch (MySqlException ex)
                {
                    Err += "Error: " + ex.Message.ToString();
                    ErrNumber = ex.Number;
                }
                closeConnection();
    
                return ds.Tables[0];
            }
    
            public void UpdateDBDataset()
            {
                try
                {
                    dt = ds.Tables[0];
                    DataTable changes = dt.GetChanges();
                    da.Update(changes);
                    dt.AcceptChanges();
                }
                catch (MySqlException ex)
                {
                    Err += "Error: " + ex.Message.ToString();
                    ErrNumber = ex.Number;
                }
            }
    
            public void DeleteDBDataset(int primarykey)
            {
                try
                {
                    ds.Tables[0].Rows[primarykey].Delete();
                }
                catch (MySqlException ex)
                {
                    Err += "Error: " + ex.Message.ToString();
                    ErrNumber = ex.Number;
                }
            }
    
            public void ExecDB(string sql)
            {
                try
                {
                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    cmd.ExecuteNonQuery();
                }
                catch (MySqlException ex)
                {
                    Err += "Error: " + ex.Message.ToString();
                    ErrNumber = ex.Number;
                }
            }
    
            public object ExecDBScalar(string sql)
            {
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                string oReturn = "";
                try
                {
                    oReturn = cmd.ExecuteScalar().ToString();
                }
                catch (MySqlException ex)
                {
                    Err += "Error: " + ex.Message.ToString();
                    ErrNumber = ex.Number;
                    oReturn = null;
                }
                return oReturn;
            }
    
            public string dbhost
            {
                get
                {
                    return pr_dbhost;
                }
                set
                {
                    pr_dbhost = value;
                }
            }
    
            public string dbuser
            {
                get
                {
                    return pr_dbuser;
                }
                set
                {
                    pr_dbuser = value;
                }
            }
    
            public string dbpass
            {
                get
                {
                    return pr_dbpass;
                }
                set
                {
                    pr_dbpass = value;
                }
            }
    
            public string dbname
            {
                get
                {
                    return pr_dbname;
                }
                set
                {
                    pr_dbname = value;
                }
            }
    }
    
    }
    Thanks,
    Pete

    P.S. This will be part of an open source .NET portal I am writing, so you are welcome to use the code if you wish.
    Last edited by digioz; 09-07-2008 at 10:42 AM. Reason: Formatting

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You should really try to use the power of inheritance as opposed to those if-then cases. Don't store a potential pointer to each, store just one pointer to their base class (in your case IData) and then assign that variable with whatever connection you wish to use.

    Another thing to note is that your database type classes have a lot of common code. Put these in a common base class instead of rewriting it (copy-n-paste) for each type. Note that System.Data.IDbConnection has a method CreateCommand that creates the appropriate command (System.Data.OleDb.OleDbCommand if it's a System.Data.OleDb.OleDbConnection and so on).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User digioz's Avatar
    Join Date
    Oct 2005
    Location
    Chicago, IL
    Posts
    3
    Thank you for the suggestion Magos. I would be very greatful if you could provide a simple example to demonstrate your idea.


    Pete

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on Classes
    By rwmarsh in forum C++ Programming
    Replies: 10
    Last Post: 04-09-2006, 06:38 AM
  2. OOP Theory Question
    By Zeusbwr in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2005, 07:37 AM
  3. Replies: 7
    Last Post: 03-10-2004, 03:10 PM
  4. MFC insert into access db (CRecordset)
    By maes in forum Windows Programming
    Replies: 11
    Last Post: 12-23-2003, 07:49 PM
  5. DBComboBox and Access DB
    By dbs5150 in forum C++ Programming
    Replies: 0
    Last Post: 03-11-2002, 09:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21