473,753 Members | 8,077 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14395
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.co m> wrote in message
news:OR******** *****@TK2MSFTNG P11.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.co m> wrote in message
news:OR******** *****@TK2MSFTNG P11.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*****@discus sions.microsoft .com> wrote in message
news:2A******** *************** ***********@mic rosoft.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
1865
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 relate right now. I've been struggling for days--days!!-- on this one simple query. I really need to get past this thing and move on. Please help. I have a classic ASP page, and it gives you 4 dropdowns. You can select any or none of them....
2
1599
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 attended. therefore there is a "functions" table and because functions has a
7
12089
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 to know this number after the first FETCH, isn't it? On a side note, why queries using LIMIT are SO terribly slow, compared to cursors and sometimes even ones without LIMIT? Shouldn't LIMIT be internally implemented using cursor mechanism then?...
1
13447
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 statement could potentially return 400,000+ rows, but I really only want the first 15 records found. I can put a lower-bound (col >= "xxx") in the WHERE clause but cannot specify an upper-bound (i.e. AND col <= "yyy"); therefore, depending on the value...
4
23545
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 number of affected rows to the client, so I would assume that "set nocount off" sends the number of affected rows to the client, and therefore, is available programatically. If so, how to get that rowcount? Thanks, Richard
4
1583
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 a message box to popup and let the user choose to abort the operation or let them continue with the long databind.
4
3077
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 as question numbers) - The Survey Extra Results (As additional questions can be listed into the Questions table, has 3 columns (a link/id to the survey id, a link/id to the question id, and the answer the user gave).
15
1790
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 (based on the primary key of the detail record). I've been trying with "TOP 3", but can't get anywhere. Using Access 2000. Something like: SELECT t1.*, TOP 3 t2.*
8
18552
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);
0
8896
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9653
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
9451
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...
0
9333
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...
1
6869
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6151
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4771
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
4942
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2284
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.