How to close connection to Firebird database

Asked

Viewed 1,037 times

3

I am doing a project in Asp.net c# web Forms framework 4.5 and I did a test connection to a Firebird database, but when closing the connection it is not closing, I used the following code to open and close:

string conDDNS;
FbConnection conexaoDDNS;

 protected void Abrir_Fechar_Click(object sender, EventArgs e)
    {
        try
        {
            this.conDDNS = "DRIVER=InterBase/Firebird(r) driver;User=SYSDBA;Password=masterkey;Database=localhost:C:/AdCom/ADCOM.FDB";

            this.conexaoDDNS = new FbConnection(conDDNS);
            this.conexaoDDNS.Open();
            ListItem item = new ListItem("Conexão aberta");
            ListBox1.Items.Add(item);

            this.conexaoDDNS.Dispose();
            this.conexaoDDNS.Close();
            ListItem item2 = new ListItem("Conexão fechada");
            ListBox1.Items.Add(item2);

        }
        catch (Exception erro)
        {
            ListItem item = new ListItem(erro.ToString());
            ListBox1.Items.Add(item);
        }

    }

I’ve tried just the remote .Close() but it didn’t work, I tried to use the .Close() and the .Dispose() but it didn’t work either.

When I did it debugging I realized that by passing the command .Open() it opens the connection normally, but when it passes the command .Close() and by .Dispose() the connection remains open in Firebird.

To find out the number of open connections in Firebird I am using the command select * FROM MON$ATTACHMENTS

2 answers

1

According to the principle of unique functionality, I suggest that you separate in different ways.

public class ConnectionFirebird
{
    private static FbConnection AbreBD()
    {
        FbConnection ConexaoBanco = new FbConnection();
        try
        {
            string _conn;
            _conn = "User=SYSDBA;Password=masterkey";
            _conn += ";Database=C:\\(path_database)";
            _conn += ";Port=3050;Dialect=3;Charset=NONE;Role=;Connection lifetime=0;";
            _conn += "Connection timeout=7;Pooling=True;Packet Size=8192;Server Type=0";
            ConexaoBanco.ConnectionString = _conn;
            ConexaoBanco.Open();

            return ConexaoBanco;
        }
        catch (Exception erro)
        {
            MessageBox.Show(String.Format("Falha na Tentativa de Acesso a Base de Dados Informada);
            return ConexaoBanco;
        }
    }

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

        Conexao = AbreBD();
        Retorno = false;
        if (Conexao.State == ConnectionState.Open)
        {
            Conexao.Close();
            Retorno = true;
        }
        return Retorno;
    }
    private static void FechaBanco(IDbConnection Conn)
    {
        // Verifica se a conexão do banco de dados está aberta
        if (Conn.State == ConnectionState.Open)
        {
            Conn.Close();
        }
    }
}

*change (path_database)

-2

My friend recommend reading the code : Connecting with Firebase

I also believe that you use the code block (using) can solve your problem.

Hugs.

  • Dude I tried what this article, it was exactly what I used in the project but it also didn’t work

  • 1

    @Jhonathan your answer has to be based on the context of the question, have content and not just links, we would like to see more elaborate answers!

Browser other questions tagged

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