473,672 Members | 2,643 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Number of rows from datareader?

dew
How do I get the number of rows a datareader has? I can tell HasRows, but
not how many.

Thanks.
Jan 20 '06 #1
6 8468
you need to read read to the end with a counter.

a datareader (at least for sqlserver) is a forward only cursor returning
data from the response stream buffer. thus you can not lnow how many rows
there are until you read to the end. on a big query you can read rows while
the server is still performing the query.

this is also why you can not read return paramater from a sp until you've
read all the result sets.

-- bruce (sqlwork.com)
"dew" <de*@yahoo.co m> wrote in message
news:uN******** ******@TK2MSFTN GP09.phx.gbl...
How do I get the number of rows a datareader has? I can tell HasRows, but
not how many.

Thanks.

Jan 20 '06 #2
Hello dew,

No way other than iterating through the reader. Sometimes the overhead of
doing this is so much that I have seen people running a count query too,
before/after Read() ing the data, if needed.

You might want to post this question in microsoft.publi c.dotnet.framew ork.adonet.

HTH,
r.
How do I get the number of rows a datareader has? I can tell HasRows,
but not how many.

Thanks.

Jan 20 '06 #3
dew
Thanks, all, I guess I'll have to use a dataset.

"dew" <de*@yahoo.co m> wrote in message
news:uN******** ******@TK2MSFTN GP09.phx.gbl...
How do I get the number of rows a datareader has? I can tell HasRows, but
not how many.

Thanks.

Jan 20 '06 #4
As one poster has already, commented, you can still use a DataReader. Just
populate it with 2 resultsets, the first one being the count of rows returns.
then use the DataReader's NextResultSet() method to switch to the one
containing all the rows.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"dew" wrote:
How do I get the number of rows a datareader has? I can tell HasRows, but
not how many.

Thanks.

Jan 20 '06 #5
this is really a bad pattern. it requires running the query twice, and if a
row is inserted/deleted between queries, the wrong answer can appear. to
make the query stable you have to use exclusive lock, or select into a temp
table.

-- bruce (sqlwork.com)
"Peter Bromberg [C# MVP]" <pb*******@yaho o.nospammin.com > wrote in message
news:0A******** *************** ***********@mic rosoft.com...
As one poster has already, commented, you can still use a DataReader. Just
populate it with 2 resultsets, the first one being the count of rows
returns.
then use the DataReader's NextResultSet() method to switch to the one
containing all the rows.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"dew" wrote:
How do I get the number of rows a datareader has? I can tell HasRows,
but
not how many.

Thanks.

Jan 21 '06 #6
Good point, Bruce, if that precision is critical to the business logic
scenario.
But then, doing so would make it a "good pattern"?
Cheers,
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Bruce Barker" wrote:
this is really a bad pattern. it requires running the query twice, and if a
row is inserted/deleted between queries, the wrong answer can appear. to
make the query stable you have to use exclusive lock, or select into a temp
table.

-- bruce (sqlwork.com)
"Peter Bromberg [C# MVP]" <pb*******@yaho o.nospammin.com > wrote in message
news:0A******** *************** ***********@mic rosoft.com...
As one poster has already, commented, you can still use a DataReader. Just
populate it with 2 resultsets, the first one being the count of rows
returns.
then use the DataReader's NextResultSet() method to switch to the one
containing all the rows.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"dew" wrote:
How do I get the number of rows a datareader has? I can tell HasRows,
but
not how many.

Thanks.


Jan 21 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1170
by: Claudia Fong | last post by:
Hi, I'm using a DataReader to connect to a db and retrieve data from the db. I just wondering how can I know the numbers of rows contain in my DB for the sql statement I used? For example, if the sql statement is like this: SELECT * FROM DEPT;
14
4325
by: Jacko | last post by:
Hi guys, Say I made a SELECT statement to my sql DB that would return 50 rows that I will use a sqldatareader to access. Instead of iterating through each and every row of the datareader, I'd like to just iterate through, say, rows 20 through 30. How can one do this?
2
1569
by: Andrew | last post by:
Hey all, Have a strange one here, and being still fairly new to .NET isn't helping me understand it. I am having a problem where a DataReader doesn't return all the rows when I try to use a method from a separate class file that returns a DataReader, where when I code the DataReader in the .aspx file it does. Below are the details and code of what I am runnign into. I appreciate any help/insight you can provide.
4
14386
by: Chris Tremblay | last post by:
I am trying to figure out how to go about retrieving the number of results returned from my queries in SQL server from VB.NET without using a the Select Count(*) query. The method that I was using was the following: Take the query that I am executing, copy the query and turn it into a count query, run the count query, then execute the original query. The reason for this is so that I can implememt public paging on my website. The...
1
2201
by: ae | last post by:
My datareader looks like this where dtrItemList is the datareader and chklExceptionList is my checkboxlist. the rows come in just fine, but I need help also including the name of the columns for my rows. I need them listed only one time on top. Any idea? Thanks in advance. while (dtrItemList.Read()) { chklExceptionList.Items.Add(dtrItemList.ToString() + " " + dtrItemList "item_name"].ToString());
2
3742
by: Mattyw | last post by:
Hi I have a sqlcommand that returns all the rows in a column and then pass that to a datareader. I am new to VS.Net and so far I can only return the first row in the first column using label1.text = reader(0).tostring
3
2621
by: hazz | last post by:
The datareader below contains two rows of two columns but in the for loop, the values for only the first row are getting printed twice. How do I get to the values of the second row? Thanks. -hazz Dim dTable As DataTable Dim dsBlock As New DataSet Dim rowBlock As DataRow Dim myColumn As DataColumn Dim iCol As Integer = 0
8
18546
by: trint | last post by:
I have a select statement that all I want to do is get the number of rows returned in my query: string strSQLAccountInfo1 = "select * " + "FROM orders " + "where user_id = '" + Class1.loginUserid.Trim() + "' "; SqlCommand cmdAL = new SqlCommand(strSQLAccountInfo1, cnSQLAccountInfo1);
2
3753
by: Andy B | last post by:
How would you find out if a linq table has 0 rows in it? I have this code: NewsContext.V_News() '*** linq table to be tested for 0 rows Any ideas?
0
8932
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8832
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8617
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8686
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7449
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4424
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2821
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2071
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.