When you have a SqlDataReader object, you can count only the records when iterating the reader:
int iCount=0;
while(myReader.Read())
{
iCount++;
}
myReader.Close();
If you are interested to count only the records returned by a query then use ExecuteScalar() instead of using a DataReader object:
string mySQL="select ....";
string myScalarQuery= "select count(*) as NumberOfRecords from (" + sSQL + ") as t1";
Or what I did was, I added a Count(*) column to the SQL SELECT so each row will contain an additional column that has # of rows returned, eventhough the data is redundant, we can avoid an additional call.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.