Exemplo n.º 1
0
        private void CreateConnectionString(IConnectionBuilder builder)
        {
            string            _server    = builder.Server()?.Trim();
            string            _username  = builder.Username()?.Trim();
            string            _database  = builder.Database()?.Trim();
            string            _password  = builder.Password()?.Trim();
            int               _port      = builder.Port();
            EncryptionOptions _encrypted = builder.Encryption();

            if (_server == null || _server.Length == 0)
            {
                throw new MysqlConnectionFailedException(MysqlConnectionFailedReasons.NoServer);
            }
            if (_username == null || _username.Length == 0)
            {
                throw new MysqlConnectionFailedException(MysqlConnectionFailedReasons.NoUsername);
            }
            if (_database == null || _database.Length == 0)
            {
                throw new MysqlConnectionFailedException(MysqlConnectionFailedReasons.NoDatabase);
            }

            StringBuilder connectionStringBuilder = new StringBuilder();

            connectionStringBuilder.AppendFormat("Server={0};", _server);
            if (_port != 3306 && _port != 0)
            {
                connectionStringBuilder.AppendFormat("Port={0};", _port);
            }
            connectionStringBuilder.AppendFormat("Uid={0};", _database);
            connectionStringBuilder.AppendFormat("Database={0};", _username);
            if (_password != null && _password.Length > 0)
            {
                connectionStringBuilder.AppendFormat("Pwd={0};", _password);
            }
            if (_encrypted != EncryptionOptions.None)
            {
                connectionStringBuilder.AppendFormat("SslMode={0};", _encrypted == EncryptionOptions.SSLPreferred ? "Preferred" : "Required");
            }

            string cs = connectionStringBuilder.ToString();

            using (var connection = new MySql.Data.MySqlClient.MySqlConnection())
            {
                connection.ConnectionString = cs;
                try
                {
                    connection.Open();
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("Access denied for user"))
                    {
                        throw new MysqlConnectionFailedException(MysqlConnectionFailedReasons.LoginFailed);
                    }
                    else if (ex.Message.Contains("Unable to connect to any of the specified MySQL hosts"))
                    {
                        throw new MysqlConnectionFailedException(MysqlConnectionFailedReasons.ConnectionFailed);
                    }
                    else if (ex.Message.Contains("does not support SSL connections"))
                    {
                        throw new MysqlConnectionFailedException(MysqlConnectionFailedReasons.EncryptionFailed);
                    }
                    else
                    {
                        throw new MysqlConnectionFailedException(MysqlConnectionFailedReasons.NotSure);
                    }
                }
            }
            this.ConnectionString = cs;
            this.Database         = _database;
        }