/// <summary> /// Same as <see cref="Connect(string, string)"/>(string, string) only this uses an enumeration. /// </summary> /// <param name="driver">The driver enumeration for you DBMS system</param> /// <param name="pluginName">The name of the plugin</param> /// <param name="connectionString">A valid connection string for you DBMS</param> /// <returns></returns> public bool Connect(dbDriver driver, string pluginName, string connectionString) { Reset(); try { string dbName; int index; this._connectionString = connectionString.Replace("\"", ""); this._driver = driver; switch (_driver) { case dbDriver.SQL: ConnectUsingOleDb(_driver, _connectionString); this._driverString = MyMetaDrivers.SQL; this.StripTrailingNulls = false; this.requiredDatabaseName = true; ClassFactory = new MyMeta.Sql.ClassFactory(); break; case dbDriver.Oracle: ConnectUsingOleDb(_driver, _connectionString); this._driverString = MyMetaDrivers.Oracle; this.StripTrailingNulls = false; this.requiredDatabaseName = true; ClassFactory = new MyMeta.Oracle.ClassFactory(); break; case dbDriver.Access: ConnectUsingOleDb(_driver, _connectionString); this._driverString = MyMetaDrivers.Access; this.StripTrailingNulls = false; this.requiredDatabaseName = false; ClassFactory = new MyMeta.Access.ClassFactory(); break; case dbDriver.MySql: ConnectUsingOleDb(_driver, _connectionString); this._driverString = MyMetaDrivers.MySql; this.StripTrailingNulls = true; this.requiredDatabaseName = true; ClassFactory = new MyMeta.MySql.ClassFactory(); break; case dbDriver.MySql2: using (MySqlConnection mysqlconn = new MySqlConnection(_connectionString)) { mysqlconn.Close(); mysqlconn.Open(); this._defaultDatabase = mysqlconn.Database; } this._driverString = MyMetaDrivers.MySql2; this.StripTrailingNulls = true; this.requiredDatabaseName = true; ClassFactory = new MyMeta.MySql5.ClassFactory(); break; case dbDriver.DB2: ConnectUsingOleDb(_driver, _connectionString); this._driverString = MyMetaDrivers.DB2; this.StripTrailingNulls = false; this.requiredDatabaseName = false; ClassFactory = new MyMeta.DB2.ClassFactory(); break; case dbDriver.ISeries: ConnectUsingOleDb(_driver, _connectionString); this._driverString = MyMetaDrivers.ISeries; this.StripTrailingNulls = false; this.requiredDatabaseName = false; ClassFactory = new MyMeta.ISeries.ClassFactory(); break; case dbDriver.Pervasive: ConnectUsingOleDb(_driver, _connectionString); this._driverString = MyMetaDrivers.Pervasive; this.StripTrailingNulls = false; this.requiredDatabaseName = false; ClassFactory = new MyMeta.Pervasive.ClassFactory(); break; case dbDriver.PostgreSQL: using (NpgsqlConnection cn = new Npgsql.NpgsqlConnection(_connectionString)) { cn.Open(); this._defaultDatabase = cn.Database; } this._driverString = MyMetaDrivers.PostgreSQL; this.StripTrailingNulls = false; this.requiredDatabaseName = false; ClassFactory = new MyMeta.PostgreSQL.ClassFactory(); break; case dbDriver.PostgreSQL8: using (NpgsqlConnection cn8 = new Npgsql.NpgsqlConnection(_connectionString)) { cn8.Open(); this._defaultDatabase = cn8.Database; } this._driverString = MyMetaDrivers.PostgreSQL8; this.StripTrailingNulls = false; this.requiredDatabaseName = false; ClassFactory = new MyMeta.PostgreSQL8.ClassFactory(); break; case dbDriver.Firebird: using (FbConnection cn1 = new FirebirdSql.Data.FirebirdClient.FbConnection(_connectionString)) { cn1.Open(); dbName = cn1.Database; } try { index = dbName.LastIndexOfAny(new char[] { '\\' }); if (index >= 0) { this._defaultDatabase = dbName.Substring(index + 1); } } catch { } this._driverString = MyMetaDrivers.Firebird; this.StripTrailingNulls = false; this.requiredDatabaseName = false; ClassFactory = new MyMeta.Firebird.ClassFactory(); break; case dbDriver.Interbase: using (FbConnection cn2 = new FirebirdSql.Data.FirebirdClient.FbConnection(_connectionString)) { cn2.Open(); this._defaultDatabase = cn2.Database; } this._driverString = MyMetaDrivers.Interbase; this.StripTrailingNulls = false; this.requiredDatabaseName = false; ClassFactory = new MyMeta.Firebird.ClassFactory(); break; case dbDriver.SQLite: using (SQLiteConnection sqliteConn = new SQLiteConnection(_connectionString)) { sqliteConn.Open(); dbName = sqliteConn.Database; if (!string.IsNullOrEmpty(dbName)) this._defaultDatabase = dbName; } this._driverString = MyMetaDrivers.SQLite; this.StripTrailingNulls = false; this.requiredDatabaseName = false; ClassFactory = new MyMeta.SQLite.ClassFactory(); break; #if !IGNORE_VISTA case dbDriver.VistaDB: try { MyMeta.VistaDB.MetaHelper mh = new MyMeta.VistaDB.MetaHelper(); dbName = mh.LoadDatabases(_connectionString); if (dbName == "") return false; this._defaultDatabase = dbName; this._driverString = MyMetaDrivers.VistaDB; this.StripTrailingNulls = false; this.requiredDatabaseName = false; ClassFactory = new MyMeta.VistaDB.ClassFactory(); } catch { throw new Exception("Invalid VistaDB connection or VistaDB not installed"); } break; #endif case dbDriver.Advantage: ConnectUsingOleDb(_driver, _connectionString); this._driverString = MyMetaDrivers.Advantage; this.StripTrailingNulls = false; this.requiredDatabaseName = false; ClassFactory = new MyMeta.Advantage.ClassFactory(); string[] s = this._defaultDatabase.Split('.'); this._defaultDatabase = s[0]; break; case dbDriver.Plugin: IMyMetaPlugin plugin; using (IDbConnection connection = this.GetConnectionFromPlugin(pluginName, _connectionString, out plugin)) { if (connection != null) connection.Open(); dbName = connection.Database; if (!string.IsNullOrEmpty(plugin.DefaultDatabase) || string.IsNullOrEmpty(dbName)) dbName = plugin.DefaultDatabase; if (!string.IsNullOrEmpty(dbName)) this._defaultDatabase = dbName; } this._driverString = pluginName; //this.StripTrailingNulls = plugin.StripTrailingNulls; //this.requiredDatabaseName = plugin.RequiredDatabaseName; ClassFactory = new MyMeta.Plugin.ClassFactory(plugin); break; case dbDriver.None: this._driverString = MyMetaDrivers.None; break; } } catch (OleDbException ex) { this._lastConnectionException = ex; foreach (OleDbError error in ex.Errors) { if (this._lastConnectionError != string.Empty) this._lastConnectionError += Environment.NewLine; this._lastConnectionError += ex; } } catch (Exception ex) { this._lastConnectionException = ex; this._lastConnectionError = ex.Message; } _isConnected = true; return true; }
public IDbConnection BuildConnection(string driver, string connectionString) { IDbConnection conn = null; switch(driver.ToUpper()) { case MyMetaDrivers.MySql2: conn = new MySqlConnection(connectionString); break; case MyMetaDrivers.PostgreSQL: case MyMetaDrivers.PostgreSQL8: conn = new Npgsql.NpgsqlConnection(connectionString); break; case MyMetaDrivers.Firebird: case MyMetaDrivers.Interbase: conn = new FirebirdSql.Data.FirebirdClient.FbConnection(connectionString); break; case MyMetaDrivers.SQLite: conn = new SQLiteConnection(connectionString); break; #if !IGNORE_VISTA case MyMetaDrivers.VistaDB: try { MyMeta.VistaDB.MetaHelper mh = new MyMeta.VistaDB.MetaHelper(); conn = mh.GetConnection(connectionString); } catch { throw new Exception("Invalid VistaDB connection or VistaDB not installed"); } break; #endif default: if (Plugins.ContainsKey(driver)) { conn = this.GetConnectionFromPlugin(driver, connectionString); } else { conn = new OleDbConnection(connectionString); } break; } return conn; }