/// <summary>
        /// Tests the <see cref="ImportConnectionInfo"/> connection.
        /// </summary>
        /// <returns><c>true</c> if all connection parameters are valid to stablish the connection.</returns>
        public bool TestConnection()
        {
            if (_connection == null)
            {
                ConnectionInfoError = ConnectionInfoErrorType.WorkbenchConnectionDoesNotExist;
                return(false);
            }

            Exception connectionException;
            bool      connectionIsValid = _connection.TestConnection(out connectionException);

            if (connectionException != null)
            {
                ConnectionInfoError = ConnectionInfoErrorType.ConnectionRefused;
            }

            return(connectionIsValid);
        }
示例#2
0
        /// <summary>
        /// Tests the given connection to check if it can successfully connect to the corresponding MySQL instance.
        /// </summary>
        /// <param name="connection">MySQL Workbench connection to a MySQL server instance selected by users.</param>
        /// <param name="displayErrorOnEmptyPassword">Flag indicating whether errors caused by a blank or null password are displayed to the user.</param>
        /// <returns>Enumeration indicating the result of the connection test.</returns>
        public static TestConnectionResult TestConnectionAndReturnResult(this MySqlWorkbenchConnection connection, bool displayErrorOnEmptyPassword)
        {
            if (connection == null)
            {
                return(TestConnectionResult.None);
            }

            Globals.ThisAddIn.Application.Cursor = ExcelInterop.XlMousePointer.xlWait;
            TestConnectionResult connectionResult;
            Exception            connectionException;

            if (connection.TestConnection(out connectionException))
            {
                connectionResult = TestConnectionResult.ConnectionSuccess;
                Globals.ThisAddIn.Application.Cursor = ExcelInterop.XlMousePointer.xlDefault;
                return(connectionResult);
            }

            // If the error returned is about the connection failing the password check, it may be because either the stored password is wrong or no password.
            connectionResult = TestConnectionResult.ConnectionError;
            if (connectionException is MySqlException)
            {
                MySqlException mySqlException = connectionException as MySqlException;
                switch (mySqlException.Number)
                {
                // Connection could not be made.
                case MySqlWorkbenchConnection.MYSQL_EXCEPTION_NUMBER_SERVER_UNREACHABLE:
                    connectionResult = TestConnectionResult.HostUnreachable;
                    InfoDialog.ShowDialog(InfoDialogProperties.GetErrorDialogProperties(
                                              Resources.ConnectFailedWarningTitle,
                                              mySqlException.Message,
                                              null,
                                              mySqlException.InnerException != null ? mySqlException.InnerException.Message : null));
                    break;

                // Wrong password.
                case MySqlWorkbenchConnection.MYSQL_EXCEPTION_NUMBER_WRONG_PASSWORD:
                    connectionResult = TestConnectionResult.WrongPassword;
                    if (!string.IsNullOrEmpty(connection.Password) || displayErrorOnEmptyPassword)
                    {
                        InfoDialog.ShowDialog(InfoDialogProperties.GetWarningDialogProperties(
                                                  Resources.ConnectFailedWarningTitle,
                                                  mySqlException.Message,
                                                  null,
                                                  connection.IsSsl() ? Resources.ConnectSSLFailedDetailWarning : null));
                    }
                    break;

                // Password has expired so any statement can't be run before resetting the expired password.
                case MySqlWorkbenchConnection.MYSQL_EXCEPTION_NUMBER_EXPIRED_PASSWORD:
                    PasswordDialogFlags passwordFlags = PasswordDialog.ShowExpiredPasswordDialog(connection, false);
                    if (!passwordFlags.Cancelled)
                    {
                        connection.Password = passwordFlags.NewPassword;
                    }

                    connectionResult = passwordFlags.Cancelled ? TestConnectionResult.PasswordExpired : TestConnectionResult.PasswordReset;
                    break;

                // Any other exception.
                default:
                    InfoDialog.ShowDialog(InfoDialogProperties.GetErrorDialogProperties(
                                              Resources.ConnectFailedWarningTitle,
                                              string.Format(Resources.GenericConnectionErrorText, mySqlException.Number, mySqlException.Message),
                                              null,
                                              mySqlException.InnerException != null ? mySqlException.InnerException.Message : null));
                    break;
                }
            }
            else
            {
                InfoDialog.ShowDialog(InfoDialogProperties.GetErrorDialogProperties(
                                          Resources.ConnectFailedWarningTitle,
                                          connectionException.Message,
                                          null,
                                          connectionException.InnerException != null ? connectionException.InnerException.Message : null));
            }

            Globals.ThisAddIn.Application.Cursor = ExcelInterop.XlMousePointer.xlDefault;
            return(connectionResult);
        }