Return to Snippet

Revision: 33236
at October 7, 2010 04:19 by amitkenny


Initial Code
class DatabaseConnection
{
        private SQLiteConnection conn;
        private SQLiteCommand sqlCommand;
        private SQLiteDataAdapter DB;
        private DataSet DS = new DataSet();

    public DatabaseConnection()
    {
        conn = new SQLiteConnection("DataSource=ProfilerPro.s3db;New=False;Compress=True;");
    }

    public SQLiteConnection Openconn()
    {
       if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken)
       {
                conn.Open();
       }
            return conn;
    }

    public SQLiteConnection Closeconn()
    {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
            return conn;
    }

    public void ExecuteQuery(string txtQuery)
    {
           

            SQLiteCommand cmd = new SQLiteCommand();
            try
            {
                cmd.Connection = Openconn();
                cmd.CommandText = txtQuery;
                cmd.ExecuteNonQuery();
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            finally
            {
                cmd = null;
            }
   }

        public DataSet returnDataSet(string txtQuery)
        {
            conn.Open();
            sqlCommand = conn.CreateCommand();
            DB = new SQLiteDataAdapter(txtQuery, conn);
            DS.Reset();
            DB.Fill(DS);
            conn.Close();
            return (DS);
        }

        
        public DataTable returnDataTable(string txtQuery)
        {
            SQLiteCommand cmd = new SQLiteCommand();
            SQLiteDataAdapter adap;
            try
            {
                cmd.Connection = Openconn();
                cmd.CommandText = txtQuery;
                adap = new SQLiteDataAdapter(cmd);
                DataTable dt = new DataTable();
                adap.Fill(dt);

                return dt;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            finally
            {
                cmd = null;
            }
        }

        public SQLiteDataReader returnDataReader(string txtQuery)
        {
            SQLiteCommand cmd = new SQLiteCommand();
            try
            {
                cmd.Connection = Openconn();
                cmd.CommandText = txtQuery;
                SQLiteDataReader rd;
                rd = cmd.ExecuteReader();
                return rd;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            finally
            {
                cmd = null;
            }
        }
    }

Initial URL

                                

Initial Description
Database connection class to be easily incorporated into any db based softwares, using SQLite 3

Initial Title
Database Connection Class

Initial Tags
database

Initial Language
C#