Number of registers returned

  • Thread starter Thread starter Lourenço Teodoro
  • Start date Start date
L

Lourenço Teodoro

After executing the ExecuteReader command fom IDbCommand, what is the best
way to get the number of registers returned by the query?

Thanks in advance,
Lourenço.
 
If you have the 1.1 framework, you can use the DataReader.HasRows property
to determine if you have rows or not. From there, if you can to know the
total ,you have to iterate through the datareader

int i = 0;
while(rdr.Read()){

i++;
}
 
That works. However, once you're run through your datareader, you've used
up your datareader and you can't get data from it anymore.

another option...

Set your command's commandText to be something like:
cmd.commandtext="select count(*) from Customers; Select * from Customers"

int RowCount;
if(rdr.Read) RowCount=rdr.GetInt64(0);
rdr.NextResult;
....now you can get the data
 
David:

I'd agree with you that it would work on Some RDMBS implementations, but not
ones that don't support batched queries. Moreover this causes two queries
to be fired and that in most instances you can usually piggyback your
iterator on whatever you need to do with the reader. I'd have recommended a
return_value or output param for efficiency, but they aren't a solution of
all IDBCommand objects b/c that's not going to be much fun using such
constructs if Excel or a .csv file is your datasource.

Bill

www.devbuzz.com
www.knowdotnet.com
 
Thank you for your reply. The solution with the loop is very simple and I
had thought about that before; my only concern is the performance; I do not
know whether the reader only moves an internal "pointer" (or reference) or
whether it sets a lot of variables. Does anyone has experience with this?

Regards,
Lourenço.
 
Unfortunately the solution with the for loop does not work for me. I want to
be able to count the amount of registers before iterating over the data. The
Read method moves forward and I cannot go back once I have called it, so I
would have to keep the returned data on an internal buffer, what would
consume more memory and would require extra code to control it.

Does anyone have any other idea?

Lourenço.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top