Error: connection to database already opened

Asked

Viewed 736 times

0

I am creating a method to test a connection with the Postgresql database, but when calling the method that opens the connection an error happens saying that the connection is already open and does not work the 'if' for checking the connection status.

Exception : "Connection already open".

public static bool TestaConn()
{
    bool Retorno = false;

    ConexaoBanco = AbreBD();

    if (ConexaoBanco.State == ConnectionState.Open)
    {
        ConexaoBanco.Close();
        Retorno = true;
    }
    return Retorno;
}

Method that opens connection, the connection string being a global variable:

public static NpgsqlConnection AbreBD()
    {
        try
        {
            if (ConexaoBanco.State == ConnectionState.Closed)
            {
               ConexaoBanco.ConnectionString = ConfGeral.StringConnexaoGeral;
               ConexaoBanco.Open();                    
            }

            return ConexaoBanco;

        }
        catch (Exception erro)
        {
            MessageBox.Show(String.Format("Falha na Tentativa de Acesso a Base de Dados Informada:\n{0}\n \nSE PRECISAR ENTRE EM CONTATO COM NOSSO SUPORTE", erro.Message), "e-SistemIntegra", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return ConexaoBanco;
        }
    }
  • Put the class code ConexaoBanco, please

  • 1

    The database is only a static variable of the type Npgsqlconnection and both the Connected variable and these methods are within the same class. * public class Databaseccess { private Static Npgsqlconnection Conexaobanco = new Npgsqlconnection();

  • The answer I posted didn’t help?

  • No, keep doing the same check.

  • You want to have no problems with open connections or want to check if the connection is open?

  • Both things, I was able to get it working now, but I had to change my connection variable to a local variable in each method, being that before I was trying with a global variable Static;

  • 1

    I get it. I was going to suggest you put the code that gets the return of AbreBD() in a block using, then, when leaving the block the connection would be closed. You would ensure that whenever you finish the block the connection would be closed.

Show 2 more comments

2 answers

0

Probably the (Connectionbank.State == Connectionstate.Closed) does not serve to perform this operation, if I am not mistaken I think there is a method related to these connection classes that checks whether the connection is open ... ConexaoBanco.isOpen()

That would be it

if (ConexaoBanco.isOpen())
{
        ConexaoBanco.Close();
        Retorno = true;
}

Something like that if I’m not mistaken.

  • Connectthe bank is a static variable and does not have the isOpen extension method().

0

Change

if (ConexaoBanco.State == ConnectionState.Closed)

For

if (ConexaoBanco.State == System.Data.ConnectionState.Closed)

Documentation of enum Connectionstate.

Based response in that reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.