c# – How to close connection to Firebird database

Question:

I'm doing a project in ASP.NET C# web forms framework 4.5 and I did a connection test with a Firebird database, but when I close the connection it isn't 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 using only the .Close() command but it didn't work, I tried using .Close() and .Dispose() but it didn't work either.

When I did this by debugging I realized that when going through the .Open() command it opens the connection normally, but when it goes through the .Close() command and .Dispose() the connection remains open in firebird.

To find out the number of open connections in firebird I'm using the command select * FROM MON$ATTACHMENTS

Answer:

According to the single functionality principle, I suggest that you separate it into different methods.

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();
        }
    }
}

*alterar (path_database)

Scroll to Top