SqlDataReader RowCount?

  • Thread starter Thread starter Johnny Jörgensen
  • Start date Start date
J

Johnny Jörgensen

Is there any way of finding out how many rows that are returned in an
System.Data.SqlClient.DataReader by a SELECT clause BEFORE you start reading
the data?

The SqlDataReader object doesn't seem to have any properties that tell that
like Rows.Count etc...

The RowsAffected property always returns -1 when it's a SELECT clause. It
only works with UPDATE, INSERT and DELETE.

Cheers,
Johnny J.
 
It is something like this:

int rcds=0;

sqlcmd.CommandText="Select count(*) from myfavoritetable where
recdid='Active'";
rcds = sqlcmd.ExecuteScalar();

if(rcds>0)
{
sqlcmd.CommandText="Select Info1, Infor2, Info3 from myfavoritetable
where recdid='Active'";
myrdr = sqlcmd.ExecuteDataReader();
while(myrdr.Read())
{
//Reads and process records
}
}
else
{
errmsg = "No Records Found.";
}
 
Back
Top