Click here to Skip to main content
16,005,241 members
Home / Discussions / Database
   

Database

 
AnswerRe: update with random value Pin
Jörgen Andersson27-Nov-11 21:37
professionalJörgen Andersson27-Nov-11 21:37 
QuestionReset Forgotten Login Password Pin
Danzy8327-Nov-11 5:31
Danzy8327-Nov-11 5:31 
AnswerRe: Reset Forgotten Login Password Pin
Mycroft Holmes27-Nov-11 11:49
professionalMycroft Holmes27-Nov-11 11:49 
AnswerRe: Reset Forgotten Login Password Pin
SilimSayo28-Nov-11 6:21
SilimSayo28-Nov-11 6:21 
AnswerRe: Reset Forgotten Login Password Pin
nilschaudhari28-Nov-11 18:55
nilschaudhari28-Nov-11 18:55 
Questionbackup and restore on sql-server-2008 Pin
Gali197826-Nov-11 5:30
Gali197826-Nov-11 5:30 
AnswerRe: backup and restore on sql-server-2008 Pin
Eddy Vluggen26-Nov-11 6:28
professionalEddy Vluggen26-Nov-11 6:28 
JokeConnect to the database with more than one single layer. Pin
baymyo25-Nov-11 22:09
baymyo25-Nov-11 22:09 
Description Image
http://baymyo.com/gallery/pimage/multi_sql_code_generate_gif-50cf8.gif[^]

Open Source Code, Downlaod Click Here!

MSSQL, ORACLE, MySQL, and OleDb to establish connections to servers via a single layer.

The following block of code to show the connection layer.
C#
public class MConnection : IDisposable
    {
        #region IDisposable Members
        public void Dispose()
        {
            this.Close();
            if (this.m_ClientConnection != null)
                this.m_ClientConnection.Dispose();
            GC.SuppressFinalize(this);
        }
        #endregion

        #region --- Member ---
        IMConnection m_ClientConnection;
        internal IMConnection ClientConnection
        {
            get { return m_ClientConnection; }
        }

        private MClientProvider m_ClientProvider;
        public MClientProvider ClientProvider
        {
            get { return m_ClientProvider; }
        }

        private string m_ConnectionString;
        public string ConnectionString
        {
            get { return m_ConnectionString; }
        }

        private System.Data.ConnectionState m_State;
        public System.Data.ConnectionState State
        {
            get { return m_State; }
        }

        public System.ComponentModel.ISite Site
        {
            get
            {
                return m_ClientConnection.Site;
            }
            set
            {
                m_ClientConnection.Site = value;
            }
        }

        public string ServerVersion
        {
            get
            {
                return m_ClientConnection.ServerVersion;
            }
        }

        public string Database
        {
            get
            {
                return m_ClientConnection.Database;
            }
        }

        public string DataSource
        {
            get
            {
                return m_ClientConnection.DataSource;
            }
        }
        #endregion

        #region --- Constructor ---
        public MConnection(MClientProvider clientProvider)
        {
            this.m_ClientProvider = clientProvider;
            switch (this.m_ClientProvider)
            {
                case MClientProvider.MSSQL:
                    this.m_ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["mssqlString"].ConnectionString;
                    this.m_ClientConnection = new MSSQLConnection(this.m_ConnectionString, this.m_ClientProvider);
                    break;
                case MClientProvider.ORACLE:
                    this.m_ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["oracleString"].ConnectionString;
                    this.m_ClientConnection = new ORACLEConnection(this.m_ConnectionString, this.m_ClientProvider);
                    break;
                case MClientProvider.MySQL:
                    this.m_ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["mysqlString"].ConnectionString;
                    this.m_ClientConnection = new MySQLConnection(this.m_ConnectionString, this.m_ClientProvider);
                    break;
                case MClientProvider.OleDb:
                    this.m_ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["oledbString"].ConnectionString;
                    this.m_ClientConnection = new OLEDBConnection(this.m_ConnectionString, this.m_ClientProvider);
                    break;
            }
        }
        public MConnection(MClientProvider clientProvider, string connectionString)
        {
            this.m_ConnectionString = connectionString;
            this.m_ClientProvider = clientProvider;
            switch (this.m_ClientProvider)
            {
                case MClientProvider.MSSQL:
                    this.m_ClientConnection = new MSSQLConnection(this.m_ConnectionString, this.m_ClientProvider);
                    break;
                case MClientProvider.ORACLE:
                    this.m_ClientConnection = new ORACLEConnection(this.m_ConnectionString, this.m_ClientProvider);
                    break;
                case MClientProvider.MySQL:
                    this.m_ClientConnection = new MySQLConnection(this.m_ConnectionString, this.m_ClientProvider);
                    break;
                case MClientProvider.OleDb:
                    this.m_ClientConnection = new OLEDBConnection(this.m_ConnectionString, this.m_ClientProvider);
                    break;
            }
        }
        #endregion

        #region --- Methods ---
        public bool Open()
        {
            try
            {
                if (this.m_ClientConnection != null)
                {
                    this.m_ClientConnection.Open();
                    this.m_State = this.m_ClientConnection.State;
                    return this.m_State.Equals(System.Data.ConnectionState.Open);
                }
                else
                    return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Close()
        {
            try
            {
                if (this.m_ClientConnection != null)
                {
                    this.m_ClientConnection.Close();
                    this.m_State = System.Data.ConnectionState.Closed;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void BeginTransaction()
        {
            this.m_ClientConnection.BeginTransaction();
        }

        public void BeginTransaction(System.Data.IsolationLevel iso)
        {
            this.m_ClientConnection.BeginTransaction(iso);
        }

        public System.Runtime.Remoting.ObjRef CreateObjRef(Type requestedType)
        {
            return this.m_ClientConnection.CreateObjRef(requestedType);
        }

        public new object GetHashCode()
        {
            return m_ClientConnection.GetHashCode();
        }

        public object InitializeLifetimeService()
        {
            return this.m_ClientConnection.InitializeLifetimeService();
        }

        public object GetLifetimeService()
        {
            return this.m_ClientConnection.GetLifetimeService();
        }

        public void ChangeDatabase(string database)
        {
            this.m_ClientConnection.ChangeDatabase(database);
        }

        public System.Data.DataTable GetSchema()
        {
            return this.m_ClientConnection.GetSchema();
        }

        public System.Data.DataTable GetSchema(string collectionName)
        {
            return this.m_ClientConnection.GetSchema(collectionName);
        }

        public System.Data.DataTable GetSchema(string collectionName, string[] restrictionValues)
        {
            return this.m_ClientConnection.GetSchema(collectionName, restrictionValues);
        }

        public override bool Equals(object obj)
        {
            return this.Equals(obj);
        }

        public override string ToString()
        {
            return m_ClientConnection.ToString();
        }
        #endregion
    }


The command object!

C#
public class MCommand : IDisposable
    {
        #region IDisposable Members
        public void Dispose()
        {
            if (this.m_ClientCommand != null)
                this.m_ClientCommand.Dispose();
            GC.SuppressFinalize(this);
        }
        #endregion

        #region --- Member ---
        IMCommand m_ClientCommand;
        internal IMCommand ClientCommand
        {
            get { return m_ClientCommand; }
        }

        string m_CommandText;
        public string CommandText
        {
            get
            {
                return m_CommandText;
            }
            set
            {
                m_CommandText = value;
            }
        }

        int m_CommandTimeout = 500;
        public int CommandTimeout
        {
            get
            {
                return m_CommandTimeout;
            }
            set
            {
                m_CommandTimeout = value;
            }
        }

        System.Data.CommandType m_CommandType = System.Data.CommandType.Text;
        public System.Data.CommandType CommandType
        {
            get
            {
                return m_CommandType;
            }
            set
            {
                m_CommandType = value;
            }
        }

        MConnection m_Connection;
        public MConnection Connection
        {
            get
            {
                return m_Connection;
            }
            set
            {
                m_Connection = value;
            }
        }

        private MParameterCollection m_Parameters;
        public MParameterCollection Parameters
        {
            get { return m_Parameters; }
            set { m_Parameters = value; }
        }

        private string m_ParamPrefix;
        public string ParamPrefix
        {
            get { return m_ParamPrefix; }
            set { m_ParamPrefix = value; }
        }

        public System.ComponentModel.IContainer Container
        {
            get
            {
                return m_ClientCommand.Container;
            }
        }

        public System.ComponentModel.ISite Site
        {
            get
            {
                return m_ClientCommand.Site;
            }
            set
            {
                m_ClientCommand.Site = value;
            }
        }

        public System.Data.UpdateRowSource UpdatedRowSource
        {
            get
            {
                return m_ClientCommand.UpdatedRowSource;
            }
            set
            {
                m_ClientCommand.UpdatedRowSource = value;
            }
        }
        #endregion

        #region --- Private Constructor ---
        internal MCommand(object command, MConnection connection)
        {
            try
            {
                this.m_Connection = connection;
                switch (connection.ClientProvider)
                {
                    case MClientProvider.MSSQL:
                        this.m_ClientCommand = new MSSQLCommand(command as System.Data.SqlClient.SqlCommand);
                        break;
                    case MClientProvider.ORACLE:
                        this.m_ClientCommand = new ORACLECommand(command as System.Data.OracleClient.OracleCommand);
                        break;
                    case MClientProvider.MySQL:
                        break;
                    case MClientProvider.OleDb:
                        this.m_ClientCommand = new OLEDBCommand(command as System.Data.OleDb.OleDbCommand);
                        break;
                }
                this.m_ClientCommand.Connection = connection;
                this.m_CommandText = this.m_ClientCommand.CommandText;
                this.m_CommandType = this.m_ClientCommand.CommandType;
                this.m_CommandTimeout = this.m_ClientCommand.CommandTimeout;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        public MCommand()
        {
            this.m_Parameters = new MParameterCollection();
        }

        public MCommand(string commandText, MConnection connection)
        {
            this.m_CommandText = commandText;
            this.m_Connection = connection;
            this.m_Parameters = new MParameterCollection();
        }

        public MCommand(System.Data.CommandType commandType, string commandText, MConnection connection)
        {
            this.m_CommandType = commandType;
            this.m_CommandText = commandText;
            this.m_Connection = connection;
            this.m_Parameters = new MParameterCollection();
        }

        #region --- Private ---
        internal void CreateCommand()
        {
            try
            {
                switch (this.Connection.ClientProvider)
                {
                    case MClientProvider.MSSQL:
                        if (string.IsNullOrEmpty(m_ParamPrefix)) m_ParamPrefix = "@";
                        switch (this.CommandType)
                        {
                            case System.Data.CommandType.Text:
                                this.m_CommandText = this.m_CommandText.Replace(":", m_ParamPrefix).Replace("?", m_ParamPrefix);
                                break;
                        }
                        this.m_ClientCommand = new MSSQLCommand(this.m_CommandType, this.m_CommandText, this.m_CommandTimeout, this.m_Connection);
                        break;
                    case MClientProvider.ORACLE:
                        if (string.IsNullOrEmpty(m_ParamPrefix)) m_ParamPrefix = ":";
                        switch (this.CommandType)
                        {
                            case System.Data.CommandType.Text:
                                this.m_CommandText = this.m_CommandText.Replace("@", m_ParamPrefix).Replace("?", m_ParamPrefix);
                                break;
                        }
                        this.m_ClientCommand = new ORACLECommand(this.m_CommandType, this.m_CommandText, this.m_CommandTimeout, this.m_Connection);
                        break;
                    case MClientProvider.MySQL:
                        if (string.IsNullOrEmpty(m_ParamPrefix)) m_ParamPrefix = "?";
                        switch (this.CommandType)
                        {
                            case System.Data.CommandType.Text:
                                this.m_CommandText = this.m_CommandText.Replace("@", m_ParamPrefix).Replace(":", m_ParamPrefix);
                                break;
                        }
                        this.m_ClientCommand = new MySQLCommand(this.m_CommandType, this.m_CommandText, this.m_CommandTimeout, this.m_Connection);
                        break;
                    case MClientProvider.OleDb:
                        if (string.IsNullOrEmpty(m_ParamPrefix)) m_ParamPrefix = "@";
                        switch (this.CommandType)
                        {
                            case System.Data.CommandType.Text:
                                this.m_CommandText = this.m_CommandText.Replace(":", m_ParamPrefix).Replace("?", m_ParamPrefix);
                                break;
                        }
                        this.m_ClientCommand = new OLEDBCommand(this.m_CommandType, this.m_CommandText, this.m_CommandTimeout, this.m_Connection);
                        break;
                }
                if (this.m_Parameters != null)
                    foreach (MParameter item in this.m_Parameters)
                        CreateParameter(item.Name, item.Value, item.DbType, item.Direction);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        void CreateParameter(string name, object value, MSqlDbType dbtype, System.Data.ParameterDirection direction)
        {
            try
            {
                switch (this.Connection.ClientProvider)
                {
                    case MClientProvider.MSSQL:
                        System.Data.SqlDbType getSqlDbType = MConvert.SqlDbType(dbtype);
                        switch (getSqlDbType)
                        {
                            case System.Data.SqlDbType.Variant:
                                ((System.Data.SqlClient.SqlCommand)this.m_ClientCommand.CommandObject).Parameters.AddWithValue(m_ParamPrefix + name, (value != null) ? value : DBNull.Value).Direction = direction;
                                break;
                            default:
                                System.Data.SqlClient.SqlParameter mssqlParam = ((System.Data.SqlClient.SqlCommand)this.m_ClientCommand.CommandObject).CreateParameter();
                                mssqlParam.ParameterName = m_ParamPrefix + name;
                                if (value != null)
                                    mssqlParam.Value = value;
                                else
                                    mssqlParam.Value = DBNull.Value;
                                mssqlParam.SqlDbType = getSqlDbType;
                                mssqlParam.Direction = direction;
                                ((System.Data.SqlClient.SqlCommand)this.m_ClientCommand.CommandObject).Parameters.Add(mssqlParam);
                                break;
                        }
                        break;
                    case MClientProvider.ORACLE:
                        System.Data.OracleClient.OracleType getOracleType = MConvert.OracleDbType(dbtype);
                        switch (getOracleType)
                        {
                            case System.Data.OracleClient.OracleType.Blob:
                                ((System.Data.OracleClient.OracleCommand)this.m_ClientCommand.CommandObject).Parameters.AddWithValue(m_ParamPrefix + name, (value != null) ? value : DBNull.Value).Direction = direction;
                                break;
                            default:
                                System.Data.OracleClient.OracleParameter oracleParam = ((System.Data.OracleClient.OracleCommand)this.m_ClientCommand.CommandObject).CreateParameter();
                                oracleParam.ParameterName = m_ParamPrefix + name;
                                if (value != null)
                                    oracleParam.Value = value;
                                else
                                    oracleParam.Value = DBNull.Value;
                                oracleParam.OracleType = getOracleType;
                                oracleParam.Direction = direction;
                                ((System.Data.OracleClient.OracleCommand)this.m_ClientCommand.CommandObject).Parameters.Add(oracleParam);
                                break;
                        }
                        break;
                    case MClientProvider.MySQL:
                        MySql.Data.MySqlClient.MySqlDbType getMySqlDbType = MConvert.MySqlType(dbtype);
                        switch (getMySqlDbType)
                        {
                            case MySql.Data.MySqlClient.MySqlDbType.Blob:
                                ((MySql.Data.MySqlClient.MySqlCommand)this.m_ClientCommand.CommandObject).Parameters.AddWithValue(m_ParamPrefix + name, (value != null) ? value : DBNull.Value).Direction = direction;
                                break;
                            default:
                                MySql.Data.MySqlClient.MySqlParameter mysqlParam = ((MySql.Data.MySqlClient.MySqlCommand)this.m_ClientCommand.CommandObject).CreateParameter();
                                mysqlParam.ParameterName = m_ParamPrefix + name;
                                if (value != null)
                                    mysqlParam.Value = value;
                                else
                                    mysqlParam.Value = DBNull.Value;
                                mysqlParam.MySqlDbType = getMySqlDbType;
                                mysqlParam.Direction = direction;
                                ((MySql.Data.MySqlClient.MySqlCommand)this.m_ClientCommand.CommandObject).Parameters.Add(mysqlParam);
                                break;
                        }
                        break;
                    case MClientProvider.OleDb:
                        switch (dbtype)
                        {
                            case MSqlDbType.Date:
                            case MSqlDbType.DateTime:
                            case MSqlDbType.DateTime2:
                            case MSqlDbType.SmallDateTime:
                                value = Convert.ToDateTime(value).ToString("yyyy/MM/dd hh:mm:ss");
                                break;
                        }
                        System.Data.OleDb.OleDbType getOleDbType = MConvert.OleDBType(dbtype);
                        switch (getOleDbType)
                        {
                            case System.Data.OleDb.OleDbType.Empty:
                                ((System.Data.OleDb.OleDbCommand)this.m_ClientCommand.CommandObject).Parameters.AddWithValue(m_ParamPrefix + name, (value != null) ? value : DBNull.Value).Direction = direction;
                                break;
                            default:
                                System.Data.OleDb.OleDbParameter oledbParam = ((System.Data.OleDb.OleDbCommand)this.m_ClientCommand.CommandObject).CreateParameter();
                                oledbParam.ParameterName = m_ParamPrefix + name;
                                if (value != null)
                                    oledbParam.Value = value;
                                else
                                    oledbParam.Value = DBNull.Value;
                                oledbParam.OleDbType = getOleDbType;
                                oledbParam.Direction = direction;
                                ((System.Data.OleDb.OleDbCommand)this.m_ClientCommand.CommandObject).Parameters.Add(oledbParam);
                                break;
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region --- Methods ---
        public int ExecuteNonQuery()
        {
            CreateCommand();
            return m_ClientCommand.ExecuteNonQuery();
        }

        public object ExecuteScalar()
        {
            CreateCommand();
            return m_ClientCommand.ExecuteScalar();
        }

        public MDataAdapter ExecuteAdapter()
        {
            CreateCommand();
            return new MDataAdapter(this);
        }

        public System.Data.IDataReader ExecuteReader()
        {
            CreateCommand();
            return m_ClientCommand.ExecuteReader();
        }

        public System.Data.IDataReader ExecuteReader(System.Data.CommandBehavior commandBehavior)
        {
            CreateCommand();
            return m_ClientCommand.ExecuteReader();
        }

        public System.Runtime.Remoting.ObjRef CreateObjRef(Type requestType)
        {
            return m_ClientCommand.CreateObjRef(requestType);
        }

        public new object GetHashCode()
        {
            return m_ClientCommand.GetHashCode();
        }

        public object GetLifetimeService()
        {
            return m_ClientCommand.GetLifetimeService();
        }

        public object InitializeLifetimeService()
        {
            return m_ClientCommand.InitializeLifetimeService();
        }

        public void Prepare()
        {
            m_ClientCommand.Prepare();
        }

        public void ResetCommandTimeout()
        {
            m_ClientCommand.ResetCommandTimeout();
        }

        public void Cancel()
        {
            m_ClientCommand.Cancel();
        }

        public object Clone()
        {
            return m_ClientCommand.Clone();
        }
        #endregion
    }

GeneralRe: Connect to the database with more than one single layer. Pin
Luc Pattyn26-Nov-11 3:48
sitebuilderLuc Pattyn26-Nov-11 3:48 
GeneralRe: Connect to the database with more than one single layer. Pin
Richard MacCutchan26-Nov-11 4:00
mveRichard MacCutchan26-Nov-11 4:00 
GeneralRe: Connect to the database with more than one single layer. Pin
Luc Pattyn26-Nov-11 4:31
sitebuilderLuc Pattyn26-Nov-11 4:31 
AnswerMy vote of 2 Pin
Eddy Vluggen26-Nov-11 6:18
professionalEddy Vluggen26-Nov-11 6:18 
GeneralRe: Connect to the database with more than one single layer. Pin
Jörgen Andersson26-Nov-11 10:51
professionalJörgen Andersson26-Nov-11 10:51 
Questioni need help Pin
niara 25-Nov-11 0:49
niara 25-Nov-11 0:49 
AnswerRe: i need help Pin
Luc Pattyn25-Nov-11 1:46
sitebuilderLuc Pattyn25-Nov-11 1:46 
AnswerRe: i need help Pin
mahsa shahi1-Mar-14 7:18
mahsa shahi1-Mar-14 7:18 
QuestionPostgres 9.1 and C# app Pin
Aleonis24-Nov-11 21:58
Aleonis24-Nov-11 21:58 
AnswerRe: Postgres 9.1 and C# app Pin
Bernhard Hiller24-Nov-11 23:51
Bernhard Hiller24-Nov-11 23:51 
GeneralRe: Postgres 9.1 and C# app Pin
Aleonis25-Nov-11 0:12
Aleonis25-Nov-11 0:12 
GeneralRe: Postgres 9.1 and C# app Pin
Mycroft Holmes25-Nov-11 0:52
professionalMycroft Holmes25-Nov-11 0:52 
GeneralRe: Postgres 9.1 and C# app Pin
Aleonis25-Nov-11 1:13
Aleonis25-Nov-11 1:13 
GeneralRe: Postgres 9.1 and C# app Pin
Mycroft Holmes25-Nov-11 12:30
professionalMycroft Holmes25-Nov-11 12:30 
GeneralRe: Postgres 9.1 and C# app Pin
Aleonis25-Nov-11 22:24
Aleonis25-Nov-11 22:24 
QuestionUsing database Pin
idbee2k322-Nov-11 5:22
idbee2k322-Nov-11 5:22 
AnswerRe: Using database Pin
R. Giskard Reventlov22-Nov-11 23:11
R. Giskard Reventlov22-Nov-11 23:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.