469,275 Members | 1,818 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

SqlDataReader RowCount?

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
3 25745
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
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
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.

Similar topics

5 posts views Thread by wackyphill | last post: by
3 posts views Thread by Dimitri Furman | last post: by
4 posts views Thread by Richard G | last post: by
1 post views Thread by Arvind P Rangan | last post: by
6 posts views Thread by Varangian | last post: by
3 posts views Thread by Jon Haakon Ariansen | last post: by
3 posts views Thread by Andrus | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.