Question

I know IDataReader doesnt have count option. And you cannot count the rows in datareader and go back to read it again because It only goes forward which means you cannot rewind it. The best solution that comes to my mind is do the select statement, count the rows in datareader, then do another select statement and do whatever you want to do with the data.

This question might have been posted here many times but nobody has given a real solution to this. What is the best solutionn whithout doing a second select statement? ( Select is expensive )

Thank you

Was it helpful?

Solution

Well, it's only a problem if you need the rowcount before reading, otherwise just interating the results and counting will give you what you need. Or just read everthing into a dataset and work from there

Supposing you need the rowcount when you read the first row, you can always do something like this (not that is a good idea)

SELECT [The Real Fields],COUNT(*) as Qty
WHERE [Conditions]
UNION 
SELECT [The Real Fields],0 as Qty
WHERE [Conditions]

That way, when you read the first row you'll know how many rows you have available.

And yes, i know there may be better ways to do the SQL to get the rowcount without the union and duplicating queries.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top