Capture error when button is pressed

1

I want to make a button that when clicked is shown the SQL Server information and say that it is connected (have to configure the file in the XML Configuration File)

See below the code:

public class Functions
{
    public static void conn()
    {
        string connectionString = Conn.tank();
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            bool Open = true;
            if (Open)
            {
                connection.Open();
                Globals.UpdateLogs("Conexão testada:");
                Globals.UpdateLogs("Status da Conexão : " + connection.State);  //info
                Globals.UpdateLogs("User : " + connection.WorkstationId);  //info
                Globals.UpdateLogs("Banco de dados : " + connection.Database);  //info
                Globals.UpdateLogs("Versão SQL : " + connection.ServerVersion); //info
            }
            else
            {
                Globals.UpdateLogs("O programa não está conectado em sua DataBase, verifique as configurações");
            }

        }
    }

In case it works right when you press the button and the data is right it says more settings when the SQL Server data is wrong, it stops the program from working.

I've got a code ready so I'm just trying to make it better, but I can not do it. "If the data is wrong, show the message."

    
asked by anonymous 15.11.2016 / 20:34

1 answer

1

I do not know if I understood correctly what you want and even if this code makes sense, but it would be more or less what I think you want:

public class Functions {
    public static void conn() {
        string connectionString = Conn.tank();
        var connection = new SqlConnection(connectionString);
        try {
            connection.Open();
            Globals.UpdateLogs("Conexão testada:");
            Globals.UpdateLogs("Status da Conexão : " + connection.State);
            Globals.UpdateLogs("User : " + connection.WorkstationId);
            Globals.UpdateLogs("Banco de dados : " + connection.Database);
            Globals.UpdateLogs("Versão SQL : " + connection.ServerVersion);
        } catch (SystemException ex) {
            if (ex is InvalidOperationException || ex is SqlException || ex is ConfigurationErrorsException) {
                Globals.UpdateLogs("O programa não está conectado em sua DataBase, verifique as configurações");
            }
        } finally {
            if (connection != null) {
                ((IDisposable)connection).Dispose();
            }
        }
    }
}

Surely you have better ways of doing this, but you would have to think about design as a whole.

For C # 5 or earlier:

public class Functions {
    public static void conn() {
        string connectionString = Conn.tank();
        var connection = new SqlConnection(connectionString);
        try {
            connection.Open();
            Globals.UpdateLogs("Conexão testada:");
            Globals.UpdateLogs("Status da Conexão : " + connection.State);
            Globals.UpdateLogs("User : " + connection.WorkstationId);
            Globals.UpdateLogs("Banco de dados : " + connection.Database);
            Globals.UpdateLogs("Versão SQL : " + connection.ServerVersion);
        } catch (InvalidOperationException ex) {
            Globals.UpdateLogs("O programa não está conectado em sua DataBase, verifique as configurações");
        } catch (SqlException  ex) {
            Globals.UpdateLogs("pode por uma mensagem mais específica aqui");
        } catch (ConfigurationErrorsException ex) {
            Globals.UpdateLogs("pode por uma mensagem mais específica aqui");
        } finally {
            if (connection != null) {
                ((IDisposable)connection).Dispose();
            }
        }
    }
}
    
15.11.2016 / 20:51