BDP.NET and InterBase generators

Posted: (EET/GMT+2)

 

A Delphi application I'm developing uses an InterBase 7 database, and in that application, I need to have a simple, auto-increment counter value.

Naturally, the best way to do this with InterBase is to use generators, and then query their values using simple SQL statements like these:

SELECT GEN_ID(MyGenerator,1)
FROM RDB$DATABASE

Previously, I've used the TIBSQL component (in Win32) to query the values, and in these cases the type returned by the component has been a 64-bit integer, i.e. an Int64.

But since I've moved almost exclusively to .NET programming, I don't want to use these components anymore, but instead the BDP.NET components, or Borland Data Providers that are compatible with ADO.NET.

So, a BdpDataReader (from the Borland.Data.Provider namespace) is a good choice to read generator values, but to my surprise the value returned for the generator is not an integer value, but... a... System.Decimal!

Oh well, something's gone wrong here, but luckily the situation is easy to fix. Just read the generator value as a decimal, and then use the ToInt32 or ToInt64 static methods of the System.Decimal structure to read the value as an integer.

For example, like this:

DataReader := SQLQuery.ExecuteReader();
Try
  If (DataReader.Read()) Then Begin
    MyGenValue := System.Decimal.ToInt32(
      DataReader.GetDecimal(0));
  End;
Finally
  DataReader.Close();
End;

This is my today's tip for anybody who is trying to solve the same issue!