Пример #1
0
        /// <summary>
        /// Creates a database connection based on the specified database information object.
        /// </summary>
        internal DBConnection(DatabaseInfo databaseInfo, bool useLongTimeouts = false)
        {
            this.databaseInfo = databaseInfo;

            // Sometimes databases are slow when nightly operations are underway.
            var time    = SystemClock.Instance.GetCurrentInstant().InZone(DateTimeZoneProviders.Tzdb.GetSystemDefault()).TimeOfDay;
            var isNight = new LocalTime(22, 0) <= time || time < new LocalTime(6, 0);
            var timeout = isNight ? 120 : useLongTimeouts ? 60 : 15;

            // Build the connection string.
            string connectionString;

            if (databaseInfo is SqlServerInfo sqlServerInfo)
            {
                connectionString = "Data Source=" + (sqlServerInfo.Server ?? "(local)");
                if (sqlServerInfo.LoginName != null)
                {
                    connectionString += "; User ID=" + sqlServerInfo.LoginName;
                    connectionString += "; Password='******'".FormatWith(sqlServerInfo.Password);
                }
                else
                {
                    connectionString += "; Integrated Security=SSPI";
                }
                connectionString += "; Initial Catalog=" + sqlServerInfo.Database;
                if (!sqlServerInfo.SupportsConnectionPooling)
                {
                    connectionString += "; Pooling=false";
                }
                connectionString += "; Connect Timeout={0}".FormatWith(timeout);
            }
            else if (databaseInfo is MySqlInfo mySqlInfo)
            {
                connectionString = "Host=localhost; SSL Mode=None; User Id=root; Password=password; Initial Catalog=" + mySqlInfo.Database;
                if (!mySqlInfo.SupportsConnectionPooling)
                {
                    connectionString += "; Pooling=false";
                }
                connectionString += "; Connect Timeout={0}".FormatWith(timeout);
            }
            else if (databaseInfo is OracleInfo oracleInfo)
            {
                connectionString = "Data Source=" + oracleInfo.DataSource + "; User Id=" + oracleInfo.UserAndSchema + "; Password="******"sys" ? "; DBA Privilege=SYSDBA" : "");
                if (!oracleInfo.SupportsConnectionPooling)
                {
                    connectionString = StringTools.ConcatenateWithDelimiter("; ", connectionString, "Pooling=false");
                }
                connectionString += "; Connection Timeout={0}".FormatWith(timeout);
            }
            else
            {
                throw new ApplicationException("Invalid database information object type.");
            }

            cn = new ProfiledDbConnection(databaseInfo.CreateConnection(connectionString), MiniProfiler.Current);

            defaultCommandTimeout = timeout;
        }
Пример #2
0
        /// <summary>
        /// Creates a database connection based on the specified database information object.
        /// </summary>
        internal DBConnection(DatabaseInfo databaseInfo /*, int? timeout */)
        {
            DatabaseInfo = databaseInfo;

            // NOTE SJR: We're going to have to generate code to retrieve this value.
            timeout = timeout;
            cn      = new ProfiledDbConnection(databaseInfo.CreateConnection(), MiniProfiler.Current);
        }
Пример #3
0
        /// <summary>
        /// Creates a database connection based on the specified database information object.
        /// </summary>
        internal DBConnection(DatabaseInfo databaseInfo)
        {
            this.databaseInfo = databaseInfo;

            // Build the connection string.
            string connectionString;

            if (databaseInfo is SqlServerInfo)
            {
                var sqlServerInfo = databaseInfo as SqlServerInfo;
                connectionString = "Data Source=" + (sqlServerInfo.Server ?? "(local)");
                if (sqlServerInfo.LoginName != null)
                {
                    connectionString += "; User ID=" + sqlServerInfo.LoginName;
                    connectionString += "; Password='******'".FormatWith(sqlServerInfo.Password);
                }
                else
                {
                    connectionString += "; Integrated Security=SSPI";
                }
                connectionString += "; Initial Catalog=" + sqlServerInfo.Database;
                if (!sqlServerInfo.SupportsConnectionPooling)
                {
                    connectionString += "; Pooling=false";
                }
            }
            else if (databaseInfo is MySqlInfo)
            {
                var mySqlInfo = databaseInfo as MySqlInfo;
                connectionString = "Host=localhost; User Id=root; Password=password; Initial Catalog=" + mySqlInfo.Database;
                if (!mySqlInfo.SupportsConnectionPooling)
                {
                    connectionString += "; Pooling=false";
                }
            }
            else if (databaseInfo is OracleInfo)
            {
                var oracleInfo = databaseInfo as OracleInfo;
                connectionString = "Data Source=" + oracleInfo.DataSource + "; User Id=" + oracleInfo.UserAndSchema + "; Password="******"sys" ? "; DBA Privilege=SYSDBA" : "");
                if (!oracleInfo.SupportsConnectionPooling)
                {
                    connectionString = StringTools.ConcatenateWithDelimiter("; ", connectionString, "Pooling=false");
                }
            }
            else
            {
                throw new ApplicationException("Invalid database information object type.");
            }

            cn = new ProfiledDbConnection(databaseInfo.CreateConnection(connectionString), MiniProfiler.Current);
        }