Search code examples
c#firebirdfirebird2.5firebird-embeddedfirebird-.net-provider

Retrieve Firebird ODS version in .Net


I am using firebird embedded v 2.5 and the .net FirebirdSql.Data.FirebirdClient. I need to be able to retrieve the ODS version for a given database.

I have tried:

private string GetOds(FbConnection connection)
{
  using (var cmd = new FbCommand())
  {
    var sqlQuery = "select rdb$get_context('SYSTEM','ENGINE_VERSION') as version from RDB$DATABASE";
    cmd.CommandText = sqlQuery;
    cmd.Connection = connection;
    cmd.CommandType = CommandType.Text;

    using (var reader = cmd.ExecuteReader()) // HERE ITS WHERE THE EXCEPTION IS GENERATED.
    {
     ...
    }
  }
}

This generates an execption: {"Dynamic SQL Error\r\nSQL error code = -804\r\nFunction unknown\r\nRDB$GET_CONTEXT"}


Solution

  • You can retrieve the ODS version using the class FirebirdSql.Data.FirebirdClient.FbDatabaseInfo, it wraps an FbConnection and can be used to retrieve information about the database, for example:

    using (var connection = new FbConnection(@"User=sysdba;Password=masterkey;Database=C:\path\to\your\database.fdb;DataSource=localhost"))
    {
        connection.Open();
        var dbInfo = new FbDatabaseInfo(connection);
    
        Console.WriteLine("ODS Major: " + dbInfo.OdsVersion);
        Console.WriteLine("ODS Minor: " + dbInfo.OdsMinorVersion);
        Console.ReadLine();
    }
    

    This should work on all ODS versions supported by Firebird, contrary to RDB$GET_CONTEXT which is only supported on ODS 11.2 or higher.