Exemplo n.º 1
0
        /// <summary>
        /// Get IDbConnection depending on configuration file
        /// </summary>
        /// <param name="existingConnection">
        /// </param>
        /// <returns>
        /// IDbConnection to the database
        /// </returns>
        /// <exception cref="DatabaseCouldNotBeDeterminedException">
        /// Database could not be determined (check config.xml)
        /// </exception>
        /// <exception cref="ConnectionStringErrorException">
        /// Connection could not be established (check config.xml)
        /// </exception>
        public static IDbConnection GetConnection()
        {
            IDbConnection conn = null;

            if (connector == null)
            {
                if (sqlType == "MySql")
                {
                    connector = new MySQLConnector(ConnectionStringMySql);
                }

                if (sqlType == "MsSql")
                {
                    connector = new MSSqlConnector(ConnectionStringMssql);
                }

                if (sqlType == "PostgreSQL")
                {
                    connector = new NpgsqlConnector(ConnectionStringPostGreSql);
                }
            }

            if (connector == null)
            {
                throw new DatabaseCouldNotBeDeterminedException("Could not determine your database");
            }

            conn = connector.GetConnection();

            if (conn == null)
            {
                throw new ConnectionStringErrorException("ConnectionString error");
            }

            conn.Open();

            return(conn);
        }