How to check if a result contains rows? (FbDataReader.HasRows always returns true!)

StackOverflow https://stackoverflow.com/questions/21927499

  •  14-10-2022
  •  | 
  •  

Domanda

I am using the Firebird ADO.NET Data Provider and before I pass the reader on to a consuming service I would like to determine whether any rows were returned. Consider the following snippet:

FbCommand cmd = GetSomeCommandFromTheEther();
FbDataReader reader = cmd.ExecuteReader();

if (reader.HasRows)
    DoSomethingWith(reader);
else
    TellTheUserWeGotNothing();

What I've now learned is that FbDataReader.HasRows always returns True. In fact looking at the source code it would appear it is just a wrapper for FbDataReader.command.IsSelectCommand, not only useless, it makes the property name "HasRows" a complete misnomer.

In any event, how can I find out whether a given query has rows, without advancing the record pointer? Note that I want to pass the reader off to an external service; if I call FbDataReader.Read() to check its result, I will consume a row and DoSomethingWith() will not get this first row.

È stato utile?

Soluzione

I am afraid you have stumbled on a Firebird limitation. As stated in following Firebird FAQ link:

Why FbDataReader.HasRows returns always true?

The FbDataReader.HasRows property is implemented for compatibility only. It returns always true because Firebird doesn't have a way for know if a query returns rows of not without fetching the data.

There is already a mention of this in the Firebird Tracker. Check the issue DNET-305.

On the other hand, in .NET, it seems OleDbDataReader and SqlDataReader, which inherit from DbDataReader have the same problem, as stated in this MSDN link.

Since FbDataReader inherits from the same class as those, you might want to consider one of the workarounds that Microsoft suggests in its MSDN article, which is to perform first a select count(*). Granted, that is unelegant and a waste of time and resources but at least it could help you out.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top