コード例 #1
0
        public Boolean Connect(bool displayErrorPopup = false)
        {
            DbProviderFactory factory;
            string            connectConnectionString = string.Empty;

            if (Database.DBType != eDBTypes.Cassandra && Database.DBType != eDBTypes.Couchbase && Database.DBType != eDBTypes.MongoDb)
            {
                connectConnectionString = GetConnectionString();
            }
            try
            {
                switch (Database.DBType)
                {
                case eDBTypes.MSSQL:
                    oConn = new SqlConnection();
                    oConn.ConnectionString = connectConnectionString;
                    oConn.Open();
                    break;

                case eDBTypes.Oracle:
                    //TODO: Oracle connection is deprecated use another method - Switched to ODP.NET
                    //Try Catch for Connecting DB Which having Oracle Version Less then 10.2
                    try
                    {
                        oConn = WorkSpace.Instance.TargetFrameworkHelper.GetOracleConnection(connectConnectionString);
                        oConn.Open();
                        break;
                    }
                    catch (Exception e)
                    {
                        String Temp = e.Message;
                        //if (Temp.Contains ("ORA-03111"))
                        if (Temp.Contains("ORA-03111"))
                        {
                            oConn = SqlClientFactory.Instance.CreateConnection();
                            oConn.ConnectionString = "Provider=msdaora;" + connectConnectionString;
                            oConn.Open();
                            break;
                        }
                        else
                        {
                            throw e;
                        }
                    }

                case eDBTypes.MSAccess:


                    oConn = WorkSpace.Instance.TargetFrameworkHelper.GetMSAccessConnection();
                    oConn.ConnectionString = connectConnectionString;
                    oConn.Open();
                    break;

                case eDBTypes.DB2:
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                    {
                        String DB2Cpath = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\IBM\DB2\GLOBAL_PROFILE", "DB2PATH", "DNE");

                        if (System.IO.Directory.Exists(DB2Cpath))
                        {
                            var DLL = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + @"DLLs\IBM.Data.DB2.dll");

                            var class1Type = DLL.GetType("IBM.Data.DB2.DB2Connection");

                            //Now you can use reflection or dynamic to call the method. I will show you the dynamic way
                            object[] param = new object[1];
                            param[0] = connectConnectionString;
                            dynamic c = Activator.CreateInstance(class1Type, param);
                            oConn = (DbConnection)c;
                            oConn.Open();
                        }
                        else
                        {
                            throw new DllNotFoundException("DB2 Connect or IBM DB2 Drivers not installed.");
                        }
                    }
                    else
                    {
                        throw new PlatformNotSupportedException("DB2 Connections are provided only on Windows Operationg System");
                    }
                    break;

                case eDBTypes.PostgreSQL:
                    oConn = new NpgsqlConnection(connectConnectionString);
                    oConn.Open();
                    break;

                case eDBTypes.Cassandra:
                    GingerCassandra CassandraDriver = new GingerCassandra(Database);
                    bool            isConnection;
                    isConnection = CassandraDriver.Connect();
                    if (isConnection == true)
                    {
                        LastConnectionUsedTime = DateTime.Now;
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }

                case eDBTypes.Couchbase:
                    GingerCouchbase CouchbaseDriver = new GingerCouchbase(Database);
                    bool            isConnectionCB;
                    isConnectionCB = CouchbaseDriver.Connect();
                    if (isConnectionCB == true)
                    {
                        LastConnectionUsedTime = DateTime.Now;
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }


                case eDBTypes.MySQL:
                    oConn = new MySqlConnection();
                    oConn.ConnectionString = connectConnectionString;
                    oConn.Open();
                    break;

                case eDBTypes.MongoDb:
                    bool          isConnectionMDB;
                    GingerMongoDb MongoDriver = new GingerMongoDb(Database);
                    isConnectionMDB = MongoDriver.Connect();
                    if (isConnectionMDB == true)
                    {
                        LastConnectionUsedTime = DateTime.Now;
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }

                default:
                    //not implemented
                    break;
                }
                if ((oConn != null) && (oConn.State == ConnectionState.Open))
                {
                    LastConnectionUsedTime = DateTime.Now;
                    return(true);
                }
            }
            catch (Exception e)
            {
                Reporter.ToLog(eLogLevel.ERROR, "DB connection failed, DB type: " + Database.DBType.ToString() + "; Connection String =" + HidePasswordFromString(connectConnectionString), e);
                throw (e);
            }
            return(false);
        }
コード例 #2
0
ファイル: Database.cs プロジェクト: lanicon/Ginger
        public Boolean Connect(bool displayErrorPopup = false)
        {
            DbProviderFactory factory;
            string            connectConnectionString = string.Empty;

            if (DBType != eDBTypes.Cassandra && DBType != eDBTypes.Couchbase && DBType != eDBTypes.MongoDb)
            {
                connectConnectionString = GetConnectionString();
            }
            try
            {
                switch (DBType)
                {
                case eDBTypes.MSSQL:
                    oConn = new SqlConnection();
                    oConn.ConnectionString = connectConnectionString;
                    oConn.Open();
                    break;

                case eDBTypes.Oracle:
                    //TODO: Oracle connection is deprecated use another method - Switched to ODP.NET
                    //Try Catch for Connecting DB Which having Oracle Version Less then 10.2
                    try
                    {
                        var      DLL        = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + @"Oracle.ManagedDataAccess.dll");
                        var      class1Type = DLL.GetType("Oracle.ManagedDataAccess.Client.OracleConnection");
                        object[] param      = new object[1];
                        param[0] = connectConnectionString;
                        dynamic c = Activator.CreateInstance(class1Type, param);
                        oConn = (DbConnection)c;
                        oConn.Open();
                        break;
                    }
                    catch (Exception e)
                    {
                        String Temp = e.Message;
                        //if (Temp.Contains ("ORA-03111"))
                        if (Temp.Contains("ORA-03111"))
                        {
                            factory = DbProviderFactories.GetFactory("System.Data.OleDb");
                            oConn   = factory.CreateConnection();
                            oConn.ConnectionString = "Provider=msdaora;" + connectConnectionString;
                            oConn.Open();
                            break;
                        }
                        else if (Temp.Contains("ORA-01017"))
                        {
                            throw e;
                        }
                        else if (!System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"Oracle.ManagedDataAccess.dll"))
                        {
                            throw new Exception(GetMissingDLLErrorDescription());
                        }
                        else
                        {
                            throw e;
                        }
                    }

                case eDBTypes.MSAccess:
                    // anything better than below?
                    // TODO: working only with mdb access97, not with accmdb
                    factory = DbProviderFactories.GetFactory("System.Data.OleDb");
                    oConn   = factory.CreateConnection();
                    oConn.ConnectionString = connectConnectionString;
                    oConn.Open();
                    break;

                case eDBTypes.DB2:

                    String DB2Cpath = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\IBM\DB2\GLOBAL_PROFILE", "DB2PATH", "DNE");

                    if (System.IO.Directory.Exists(DB2Cpath))
                    {
                        var DLL = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + @"DLLs\IBM.Data.DB2.dll");

                        var class1Type = DLL.GetType("IBM.Data.DB2.DB2Connection");

                        //Now you can use reflection or dynamic to call the method. I will show you the dynamic way
                        object[] param = new object[1];
                        param[0] = connectConnectionString;
                        dynamic c = Activator.CreateInstance(class1Type, param);
                        oConn = (DbConnection)c;
                        oConn.Open();
                    }
                    else
                    {
                        throw new Exception("DB2 Connect or IBM DB2 Drivers not installed.");
                    }
                    break;

                case eDBTypes.PostgreSQL:
                    oConn = new NpgsqlConnection(connectConnectionString);
                    oConn.Open();
                    break;

                case eDBTypes.Cassandra:
                    GingerCassandra CassandraDriver = new GingerCassandra(this);
                    bool            isConnection;
                    isConnection = CassandraDriver.Connect();
                    if (isConnection == true)
                    {
                        LastConnectionUsedTime = DateTime.Now;
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }

                case eDBTypes.Couchbase:
                    GingerCouchbase CouchbaseDriver = new GingerCouchbase(this);
                    bool            isConnectionCB;
                    isConnectionCB = CouchbaseDriver.Connect();
                    if (isConnectionCB == true)
                    {
                        LastConnectionUsedTime = DateTime.Now;
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }


                case eDBTypes.MySQL:
                    oConn = new MySqlConnection();
                    oConn.ConnectionString = connectConnectionString;
                    oConn.Open();
                    break;

                case eDBTypes.MongoDb:
                    bool          isConnectionMDB;
                    GingerMongoDb MongoDriver = new GingerMongoDb(this);
                    isConnectionMDB = MongoDriver.Connect();
                    if (isConnectionMDB == true)
                    {
                        LastConnectionUsedTime = DateTime.Now;
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                if ((oConn != null) && (oConn.State == ConnectionState.Open))
                {
                    LastConnectionUsedTime = DateTime.Now;
                    return(true);
                }
            }
            catch (Exception e)
            {
                Reporter.ToLog(eLogLevel.ERROR, "DB connection failed, DB type: " + DBType.ToString() + "; Connection String =" + General.HidePasswordFromString(connectConnectionString), e);
                throw (e);
            }
            return(false);
        }