By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,494 Members | 1,136 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,494 IT Pros & Developers. It's quick & easy.

SqlDataReader RowCount?

P: n/a
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.
Aug 21 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
You can have 2 select statements in your SQL or stored proc. The first one
will return the count, and the second one will return the actual rows of data.
Then you can use the NextResultset method on the reader after getting the
count, to get the actual data.
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"Johnny Jörgensen" wrote:
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.
Aug 21 '07 #2

P: n/a
Have you got a small code sample?

Cheers,
Johnny J.
"Peter Bromberg [C# MVP]" <pb*******@yahoo.yohohhoandabottleofrum.comwrote
in message news:A6**********************************@microsof t.com...
You can have 2 select statements in your SQL or stored proc. The first one
will return the count, and the second one will return the actual rows of
data.
Then you can use the NextResultset method on the reader after getting the
count, to get the actual data.
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"Johnny Jörgensen" wrote:
>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.

Aug 21 '07 #3

P: n/a
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.";
}
Sep 15 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.