Exemple #1
0
        /// <summary>
        /// Create <see cref="DbConnection"/> instance.
        /// </summary>
        /// <summary xml:lang="zh-CN">
        /// 创建连接对象
        /// </summary>
        /// <param name="source"><see cref="Database"/></param>
        /// <param name="connectString"></param>
        /// <returns></returns>
        public static DbConnection CreateDbConnection(Database source, string connectString)
        {
            DbProviderFactory factory;

            switch (source)
            {
            case Database.MySql:
            {
                factory = DatabaseProviderFactories.GetFactory(DatabaseProviderFactories.MySqlProvider);
                break;
            }

            case Database.SqlServer:
            {
                factory = DatabaseProviderFactories.GetFactory(DatabaseProviderFactories.SqlServerProvider);
                break;
            }

            case Database.PostgreSql:
            {
                factory = DatabaseProviderFactories.GetFactory(DatabaseProviderFactories.PostgreSqlProvider);
                break;
            }

            default:
            {
                throw new SpiderException($"Unsupported database: {source}");
            }
            }

            for (int i = 0; i < 5; ++i)
            {
                try
                {
                    var connection = factory.CreateConnection();
                    if (connection != null)
                    {
                        connection.ConnectionString = connectString;
                        connection.Open();
                        return(connection);
                    }
                }
                catch (Exception e)
                {
                    if (e.Message.ToLower().StartsWith("authentication to host"))
                    {
                        Log.Logger.Error($"{e}");
                        Thread.Sleep(1000);
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            throw new SpiderException("Create connection failed");
        }
Exemple #2
0
        /// <summary>
        /// Create DbConnection from <see cref="ConnectionStringSettings"/>.
        /// </summary>
        /// <summary xml:lang="zh-CN">
        /// 通过配置创建连接对象
        /// </summary>
        /// <param name="connectionStringSettings">数据库配置对象 <see cref="ConnectionStringSettings"/></param>
        /// <returns>连接对象 <see cref="DbConnection"/></returns>
        public static DbConnection CreateDbConnection(this ConnectionStringSettings connectionStringSettings)
        {
            if (connectionStringSettings == null)
            {
                throw new SpiderException("ConnectionStringSetting is null");
            }

            if (string.IsNullOrWhiteSpace(connectionStringSettings.ConnectionString) ||
                string.IsNullOrWhiteSpace(connectionStringSettings.ProviderName))
            {
                throw new SpiderException("ConnectionStringSetting is incorrect");
            }

            var factory = DatabaseProviderFactories.GetFactory(connectionStringSettings.ProviderName);

            for (int i = 0; i < 5; ++i)
            {
                try
                {
                    DbConnection connection = factory.CreateConnection();
                    if (connection != null)
                    {
                        connection.ConnectionString = connectionStringSettings.ConnectionString;
                        if (connection.State == ConnectionState.Closed)
                        {
                            connection.Open();
                        }

                        return(connection);
                    }
                }
                catch (Exception e)
                {
                    if (e.Message.ToLower().StartsWith("authentication to host"))
                    {
                        Log.Logger.Error($"{connectionStringSettings.ConnectionString}: {e}.");
                        break;
                    }

                    if (e.Message.ToLower().StartsWith("access denied for user"))
                    {
                        Log.Logger.Error($"Access denied: {connectionStringSettings.ConnectionString}.");
                        break;
                    }
                    else
                    {
                        Log.Logger.Warning($"{connectionStringSettings.ConnectionString}: {e}");
                        Thread.Sleep(1000);
                    }
                }
            }

            return(null);
        }