473,396 Members | 1,706 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,396 software developers and data experts.

retrieving row count from query

VMI
How can I execute a "Select" query from C# so that it only returns an
integer with the amount of rows that the actual query is supposed to return?
I don't want the whole result set.

Thanks.
Nov 16 '05 #1
6 12422
Hi,

this should do the trick:

"SELECT COUNT(*) FROM my_DB.table;"

This helped me alot:
http://www.w3schools.com/sql/default.asp

Martin
VMI wrote:
How can I execute a "Select" query from C# so that it only returns an
integer with the amount of rows that the actual query is supposed to return?
I don't want the whole result set.

Thanks.

Nov 16 '05 #2
Remember to execute the query using SqlCommand.ExecuteScalar ro get the result (although in theory you could use the data reader and look the first column of the first row)

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

Hi,

this should do the trick:

"SELECT COUNT(*) FROM my_DB.table;"

This helped me alot:
http://www.w3schools.com/sql/default.asp

Martin
VMI wrote:
Nov 16 '05 #3
VMI
Thanks, it worked, in conjunction with ExecuteScalar .
I read that with the data reader you would need to bring the resultset back
to the application.
"mphanke" <mp*****@nospam.nospam> wrote in message
news:uJ**************@TK2MSFTNGP15.phx.gbl...
Hi,

this should do the trick:

"SELECT COUNT(*) FROM my_DB.table;"

This helped me alot:
http://www.w3schools.com/sql/default.asp

Martin
VMI wrote:
How can I execute a "Select" query from C# so that it only returns an
integer with the amount of rows that the actual query is supposed to
return? I don't want the whole result set.

Thanks.

Nov 16 '05 #4
VMI <vo******@yahoo.com> wrote:
Thanks, it worked, in conjunction with ExecuteScalar .
I read that with the data reader you would need to bring the resultset back
to the application.


Well, yes and no - you'd bring "the resultset" back, but that resultset
would be just a single row with a single column, because the query
doesn't retrieve the rows, only the count.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
--> if I want to get both the "resultset" and count affected rows. How can?
I must fetch to the end row and count ? another way?

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
VMI <vo******@yahoo.com> wrote:
Thanks, it worked, in conjunction with ExecuteScalar .
I read that with the data reader you would need to bring the resultset back to the application.


Well, yes and no - you'd bring "the resultset" back, but that resultset
would be just a single row with a single column, because the query
doesn't retrieve the rows, only the count.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #6
Dean L. Howen <co*****@hotpop.com> wrote:
--> if I want to get both the "resultset" and count affected rows. How can?
I must fetch to the end row and count ? another way?


No, there basically isn't another way. I asked the same thing in a Java
newsgroup a long time ago, and was shocked by the answer :(

Any time you ask for a number of rows, that information can be out of
date by the time you get the answer back. You could do a query which
returned two resultsets, one containing just the count and one
containing the actual data - but you need to be ready for there to be
more or fewer rows in the actual data than the count suggests...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

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

Similar topics

2
by: peter | last post by:
I've got a table which is used for storing votes, each row has a field with a reference to the user who's been voted for and i'd like to simply get the user ref with the most votes,or if tied,...
2
by: Belmin | last post by:
Hi all, Wanted to know what is the most efficient way of doing a select query for mysql that only returns one value. For example: $mysqli->query('select count(*) from log'); $temprec =...
2
by: Sagaert Johan | last post by:
Hi I have set the columreorder to true Is there a way to get/set the current columnorder, so i can restore the order from a saved setting ? Johan
5
by: jan axelson | last post by:
My application is using RegisterDeviceNotification() to detect attachment and removal of a USB HID-class device. The form is receiving WM_DEVICECHANGE messages with wParam set to...
5
by: Christoffer Gurell | last post by:
I want to create a program which displays large tables and makes this possible over a slow connection. The problem is that when i do a PQexec the entire retultset is transfered. I would like to...
0
by: EastendLad | last post by:
OK guys hope this isnt to below you all but im new to msql and would appreciate some help. I have the following tables forum_frm (ALIAS = F) ########################## # id_frn # title_frm # ...
4
by: assgar | last post by:
Hi I am php newbie and I don't know if I am approaching this properly. I need to creat a scheduler that list the appointment time on the left and client for a specific appointment time on the...
9
ADezii
by: ADezii | last post by:
One question which pops up frequently here at TheScripts is: 'How do I retrieve data from a Recordset once I've created it?' One very efficient, and not that often used approach, is the GetRows()...
2
by: rds80 | last post by:
In the xml document below, I would like to retrieve the distinct attributes for the element '<Bal>'. However, I haven't had any success. Here is what I have so far: <TRANS> <TRAN...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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...
0
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...

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.