Click here to Skip to main content
15,896,606 members
Articles / Programming Languages / SQL

A Helpful Database Library

Rate me:
Please Sign up or sign in to vote.
2.75/5 (4 votes)
6 Dec 20065 min read 50.3K   704   26  
This article illustrates a helpful database library.
using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using MSDASC;
using ADODB;

namespace MyDatabaseLibrary
{
    public static class MyDatabase
    {
        public static void RunSqlCommands(MySqlData _MySQLData)
        {
            if (_MySQLData.MyDatabaseProvider == DatabaseProvider.MicrosoftJet)
            {
                OleDbConnection MyOleConn = new OleDbConnection(_MySQLData.MyConnectionString);
                OleDbCommand MyOleComm = new OleDbCommand(_MySQLData.MySQLString, MyOleConn);
                MyOleComm.Connection.Open();
                MyOleComm.ExecuteNonQuery();
                MyOleComm.Connection.Close();
            }
            else
            {
                SqlConnection MySqlConn = new SqlConnection(_MySQLData.MyConnectionString);
                SqlCommand MySqlComm = new SqlCommand(_MySQLData.MySQLString, MySqlConn);
                MySqlComm.Connection.Open();
                MySqlComm.ExecuteNonQuery();
                MySqlComm.Connection.Close();
            }
        }
        
        public static void GenerateConnectionString(ref MySqlData _MySqlData)
        {
            DataLinksClass MyDataLink = new DataLinksClass();
            Connection MyADOConn = (Connection)MyDataLink.PromptNew();                
            if (MyADOConn.Provider == "Microsoft.Jet.OLEDB.4.0")
            {
                _MySqlData.MyDatabaseProvider = DatabaseProvider.MicrosoftJet;
                _MySqlData.MyConnectionString = MyADOConn.ConnectionString;
            }
            else
            {
                _MySqlData.MyDatabaseProvider = DatabaseProvider.MicrosoftSQL;
                _MySqlData.MyConnectionString  = MyADOConn.ConnectionString;
                _MySqlData.MyConnectionString  = _MySqlData.MyConnectionString.Substring(
                    (_MySqlData.MyConnectionString.IndexOf(";") + 1),
                        (_MySqlData.MyConnectionString.Length - (
                            (_MySqlData.MyConnectionString.IndexOf(";") + 1))));
            }
        }

        public static DataTable GetDataTable(MySqlData _MySQLData)
        {
            DataTable MyDatatbl = new DataTable();
            if (_MySQLData.MyDatabaseProvider == DatabaseProvider.MicrosoftJet)
            {
                OleDbDataAdapter MyOleAdpt = new OleDbDataAdapter(_MySQLData.MySQLString, _MySQLData.                                          MyConnectionString);
                MyOleAdpt.Fill(MyDatatbl);
            }
            else if (_MySQLData.MyDatabaseProvider == DatabaseProvider.MicrosoftSQL)
            {
                SqlDataAdapter MySqlAdpt = new SqlDataAdapter(_MySQLData.MySQLString, _MySQLData.                                              MyConnectionString);
                MySqlAdpt.Fill(MyDatatbl);
            }
            return MyDatatbl;
        }

        public static DataTable GetDataBaseSchema(MySqlData _MySQLData)
        {
            DataTable MyDatatbl = new DataTable();
            if (_MySQLData.MyDatabaseProvider == DatabaseProvider.MicrosoftJet)
            {
                OleDbConnection OleConn = new OleDbConnection(_MySQLData.MyConnectionString);
                OleConn.Open();
                MyDatatbl = OleConn.GetSchema("Tables");
                OleConn.Close();
            }
            else if (_MySQLData.MyDatabaseProvider == DatabaseProvider.MicrosoftSQL)
            {
                SqlConnection SqlConn = new SqlConnection(_MySQLData.MyConnectionString);
                SqlConn.Open();
                MyDatatbl = SqlConn.GetSchema("Tables");
                SqlConn.Close();
            }
            return MyDatatbl;
        }
    }

    public class MySqlData
    {
        private string _MyConnectionString;
        private string _MySQLString;
        private DatabaseProvider _MyDatabaseProvider;

        public MySqlData()
        {

        }

        public string MyConnectionString
        {
            get
            {
                return _MyConnectionString;
            }
            set
            {
                _MyConnectionString = value;
            }
        }

        public string MySQLString
        {
            get
            {
                return _MySQLString;
            }
            set
            {
                _MySQLString = value;
            }
        }

        public DatabaseProvider MyDatabaseProvider
        {
            get
            {
                return _MyDatabaseProvider;
            }
            set
            {
                _MyDatabaseProvider = value;
            }
        }
    }

    public enum DatabaseProvider
    {
        MicrosoftJet=1,
        MicrosoftSQL,
        MySQL,
        Oracle
    }
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I am a software, database, and gis developer. I love the challenge of learning new ways to code.

Comments and Discussions