473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Getting Number Of Rows returned from query

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 problem with this method is that it is putting to much stress on my SQL
Server. Is there a way that I can find the number of rows returned. The way
I figure, if you use an SQLdatareader, it knows when it has reached the end
of the result list, therefore, you should be able to have some access to the
number of results without reading through each record.

--

Thanks,
Chris Tremblay
www.mtgfanatic.com, Inc.
Nov 18 '05 #1
4 14372
Chris,
You won't be able to do it via the SQLDataReader without reading through the
records first (in which case you can simply increment a counter, or use the
solution I briefly outline in #2)

1 - Consider using a datatable. You'll easily be able to access the number
of rows via datatable.Rows.Count. Additionally, if your SQL Server is
struggling, you'll be able to cache the datatable and reduce the load (I
realize that you are probably doing a search in which case the results
aren't very cache friendly, but hey , I thought I'd throw it out there
anyways).

2 - You can access an output parameter after doing a dr.close, which could
be the @@RowCount SQL server automatically generates. I'd go into more
details, but since you need to have the datareader closed, I'll assume that
you'll have already read through it and could simply have used a
counter...so this probably is a no go.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Chris Tremblay" <Cu*************@mtgfanatic.com> wrote in message
news:OR*************@TK2MSFTNGP11.phx.gbl...
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 problem with this method is that it is putting to much stress on my SQL Server. Is there a way that I can find the number of rows returned. The way I figure, if you use an SQLdatareader, it knows when it has reached the end of the result list, therefore, you should be able to have some access to the number of results without reading through each record.

--

Thanks,
Chris Tremblay
www.mtgfanatic.com, Inc.

Nov 18 '05 #2
You could use a counter variable like so...

SQLDataReader dr = new SQLDataReader();
int counter = 0;
// other database code goes here
while ( dr.Read() )
{
counter +=;
}
Response.Write ( "Number of records is: " + counter );

Hope this helps

"Chris Tremblay" wrote:
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 problem with this method is that it is putting to much stress on my SQL
Server. Is there a way that I can find the number of rows returned. The way
I figure, if you use an SQLdatareader, it knows when it has reached the end
of the result list, therefore, you should be able to have some access to the
number of results without reading through each record.

--

Thanks,
Chris Tremblay
www.mtgfanatic.com, Inc.

Nov 18 '05 #3
After you run the query, run the statement SELECT @@ROWCOUNT

--
data mining and .net team
http://www.visual-basic-data-mining.net/forum
"Chris Tremblay" <Cu*************@mtgfanatic.com> wrote in message
news:OR*************@TK2MSFTNGP11.phx.gbl...
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 problem with this method is that it is putting to much stress on my SQL Server. Is there a way that I can find the number of rows returned. The way I figure, if you use an SQLdatareader, it knows when it has reached the end of the result list, therefore, you should be able to have some access to the number of results without reading through each record.

--

Thanks,
Chris Tremblay
www.mtgfanatic.com, Inc.

Nov 18 '05 #4
Unless I missing something this won't work.

If he is doing paging, he is probably only returning in the datareader a
subset (1 page) of the total records.

Greg
"--dweezil" <dw*****@discussions.microsoft.com> wrote in message
news:2A**********************************@microsof t.com...
You could use a counter variable like so...

SQLDataReader dr = new SQLDataReader();
int counter = 0;
// other database code goes here
while ( dr.Read() )
{
counter +=;
}
Response.Write ( "Number of records is: " + counter );

Hope this helps

"Chris Tremblay" wrote:
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 problem with this method is that it is putting to much stress on my
SQL
Server. Is there a way that I can find the number of rows returned. The
way
I figure, if you use an SQLdatareader, it knows when it has reached the
end
of the result list, therefore, you should be able to have some access to
the
number of results without reading through each record.

--

Thanks,
Chris Tremblay
www.mtgfanatic.com, Inc.

Nov 18 '05 #5

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

Similar topics

23
by: middletree | last post by:
I've seen posts here and elsewhere which read something along the lines of "PULLNG MY HAIR OUT!!!!!" or "HELLLLPPP!". Well, I know that kind of subject line isn't descriptive, but I sure can...
2
by: Gregory.Spencer | last post by:
Help, I have a query in MySQL which gets the details of members of a club. e.g. Select * from members. however, in the same query I want to return the amount of "functions" the member has...
7
by: Egor Shipovalov | last post by:
I'm implementing paging through search results using cursors. Is there a better way to know total number of rows under a cursor than running a separate COUNT(*) query? I think PostgreSQL is bound...
1
by: BF | last post by:
Hello, I am trying to find out if there is a way to limit the number of rows returned when a cursor is opened. I am using DB2 version 7 on z/OS. The SELECT statement in my DECLARE CURSOR...
4
by: Richard G | last post by:
I'm a database guy, so go easy on me here. :) How can I get the rowcount of the affected rows of a SQL statement from a stored procedure call? I know that "set nocount on" does not return the...
4
by: Ed | last post by:
I have a new requisite from a client that wants to give the user the option to about a databind based on number of returned rows. if ds.rows.count < 1000 then dg.databind() etc. They want...
4
by: dallasfreeman | last post by:
I'm looking at a quick way to get results that are displayed as rows to display as columns. I have three tables:- - The Questions for the survey - The Results of the survey (Columns are listed...
15
by: Hexman | last post by:
Hello All, How do I limit the number of detail records selected in a Master-Detail set using SQL? I want to select all master records for a date, but only the first 3 records for the details...
8
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 = '" +...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...

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.