4

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"}

Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158

2 Answers2

3

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.

Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
0

You can retrieve the ODS version from binary.

private const ushort FirebirdFlag = 0x8000;

private void DispObsVersinoFromBinary(string path)
{
    using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
    {
        int fileSize = (int)fs.Length;
        byte[] buf = new byte[1024];
        fs.Read(buf, 0, 1024);
        var obsHex = string.Join("", buf.Skip(0x12).Take(2).Select(x => x.ToString("X2")).Reverse());
        var minor = string.Join("", buf.Skip(0x40).Take(2).Select(x => x.ToString("X2")).Reverse());
        Console.WriteLine($"ODSVer:{Convert.ToInt32(obsHex, 16) & ~FirebirdFlag}");
        Console.WriteLine($"ODSMinorVer:{Convert.ToInt32(minor, 16)}");
    }
}

and other pattern
https://github.com/kowill/Sample/blob/master/fb3test/Fb3Test/Program.cs#L71-L82

kowill
  • 1