473,804 Members | 3,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataReader and RecordCount

Does anyone have any insight on how I can get a count of the records returned
after executing an ExecuteReader command? Something similar to the
..RecordCount property in Classic ASP?
Nov 19 '05 #1
9 10254
"Paul" <Pa**@discussio ns.microsoft.co m> wrote in message
news:7A******** *************** ***********@mic rosoft.com...
Does anyone have any insight on how I can get a count of the records
returned
after executing an ExecuteReader command? Something similar to the
.RecordCount property in Classic ASP?


Not natively. The DataReader is a connected object, so its record count is
not known until all the records have been read.

If you don't need to know the record count *before* you start reading the
records, you can simply increment a counter each time you read a record.

If you just want to know whether the DataReader contains records or not, use
its HasRows property (assuming you're using v1.1 of the Framework)

However, if you really need to know the number of records before you have
read them all, essentially you have two choices:

1) Return two recordsets, the first containing the number of rows e.g.

SELECT COUNT(*) FROM <table>
SELECT * FROM <table>

2) Use a DataSet...
Nov 19 '05 #2
declare 1 integer and assign the Datareader.Exec uteReader();
This method will return some integer(how many records affected / inserted) ,
work on that.
int i=Datareader.Ex ecuteReader(); --try ?

Mahesh
www.snipurl.com/guac

"Paul" <Pa**@discussio ns.microsoft.co m> wrote in message
news:7A******** *************** ***********@mic rosoft.com...
Does anyone have any insight on how I can get a count of the records returned after executing an ExecuteReader command? Something similar to the
.RecordCount property in Classic ASP?

Nov 19 '05 #3
Thanks for this but the ExecuteReader method does not return an integer?

It is my understanding that it returns a SQLDataReader object.


"¿ Mahesh Kumar" wrote:
declare 1 integer and assign the Datareader.Exec uteReader();
This method will return some integer(how many records affected / inserted) ,
work on that.
int i=Datareader.Ex ecuteReader(); --try ?

Mahesh
www.snipurl.com/guac

"Paul" <Pa**@discussio ns.microsoft.co m> wrote in message
news:7A******** *************** ***********@mic rosoft.com...
Does anyone have any insight on how I can get a count of the records

returned
after executing an ExecuteReader command? Something similar to the
.RecordCount property in Classic ASP?


Nov 19 '05 #4
Thank you for this help.

I do need the count before I loop through the records.

HasRows will not help because I need a specific count.

Submitting 2 SQL calls (SELECT COUNT(*) and regular SELECT) is what I
thought I might have to do, but was hoping for something more efficient.

Using a DataSet rather than a DataReader might be the answer, but I still do
not see any Count property that would give me the count before I looped
through the records?
"Mark Rae" wrote:
"Paul" <Pa**@discussio ns.microsoft.co m> wrote in message
news:7A******** *************** ***********@mic rosoft.com...
Does anyone have any insight on how I can get a count of the records
returned
after executing an ExecuteReader command? Something similar to the
.RecordCount property in Classic ASP?


Not natively. The DataReader is a connected object, so its record count is
not known until all the records have been read.

If you don't need to know the record count *before* you start reading the
records, you can simply increment a counter each time you read a record.

If you just want to know whether the DataReader contains records or not, use
its HasRows property (assuming you're using v1.1 of the Framework)

However, if you really need to know the number of records before you have
read them all, essentially you have two choices:

1) Return two recordsets, the first containing the number of rows e.g.

SELECT COUNT(*) FROM <table>
SELECT * FROM <table>

2) Use a DataSet...

Nov 19 '05 #5
"Paul" <Pa**@discussio ns.microsoft.co m> wrote in message
news:B0******** *************** ***********@mic rosoft.com...
Using a DataSet rather than a DataReader might be the answer, but I still
do
not see any Count property that would give me the count before I looped
through the records?


<DataSet object>.Tables[0].Rows[0].Count
Nov 19 '05 #6
"¿ Mahesh Kumar" <cy**********@g mail.com> wrote in message
news:u%******** ********@TK2MSF TNGP15.phx.gbl. ..
declare 1 integer and assign the Datareader.Exec uteReader();
This method will return some integer(how many records affected / inserted)
,
work on that.
int i=Datareader.Ex ecuteReader(); --try ?


1) ExecuteReader() is a method of the Command object, not the DataReader
object.

2) ExecuteReader() returns a DataReader object, not an integer.
Nov 19 '05 #7
"Paul" <Pa**@discussio ns.microsoft.co m> wrote in message
news:92******** *************** ***********@mic rosoft.com...
Thanks for this but the ExecuteReader method does not return an integer?

It is my understanding that it returns a SQLDataReader object.


That's correct - you should ignore that post - it's totally wrong.
Nov 19 '05 #8
"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:OG******** ******@TK2MSFTN GP09.phx.gbl...
<DataSet object>.Tables[0].Rows[0].Count


Woops, that should be

DataSet object>.Tables[0].Rows.Count
Nov 19 '05 #9
There a many ways to do that.
You can make use of "ExecuteSca lar "
if you use
Select count(*) from TableName
you will get back a single item - - so you can use ExecuteScalar, instead of
executeReader:
dim counter as integer
counter=cmdMbr. ExecuteScalar
label1.text=cou nter

Using Dataset also is a good idea but there is alos some burden vs
getting the count(*) directly using command object
Patrick

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:##******** ******@TK2MSFTN GP09.phx.gbl...
"¿ Mahesh Kumar" <cy**********@g mail.com> wrote in message
news:u%******** ********@TK2MSF TNGP15.phx.gbl. ..
declare 1 integer and assign the Datareader.Exec uteReader();
This method will return some integer(how many records affected / inserted) ,
work on that.
int i=Datareader.Ex ecuteReader(); --try ?


1) ExecuteReader() is a method of the Command object, not the DataReader
object.

2) ExecuteReader() returns a DataReader object, not an integer.

Nov 19 '05 #10

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

Similar topics

2
5074
by: Jeff Boyer | last post by:
Hello everyone, Can someone tell me why a ADO recordset.recordcount would return a -1? I have confirmed that it has records in it by writing some values to the screen. Why can't I count the number of records returned though? Please help, Jeff P.S. I am using VBScript for my code connecting to a SQL Server 2000 database
12
2975
by: scott | last post by:
In LISTING 2, I have a SPROC that returns a recordset and a recordcount in SQL QA. I can access the Recordset with no problem. How can I grab the Recordcount with ASP code at the same time I'm getting the Recordset? In QA, the RecordCount displays below the Recordset. The below USAGE code will display my SPROC results. USAGE: EXEC SELECT_WITH_PAGING 'CustomerID, ShipName', 'OrderID', 'Northwind.dbo.Orders', 3, 10, 1, '', 'OrderDate'...
4
4889
by: don.fleming | last post by:
Hi folks: Am doing a VBA SQL Select stmt with multiple rows found and not getting RecordCount > 1. I've verified there are multiple rows found by copying my SQL stmt from Debug window and pasting it to a query - retrieves many rows. I need to program to handle many Rows in VBA. Suggestions much appreciated! Here is an excerpt of my VBA code after the SQLstmt is built: Debug.Print SQLstmt Set RecSet1 = SQLtbl.OpenRecordset(SQLstmt)
1
14571
by: Tom | last post by:
Hi How to read the LAST record of datareader? Here is my code ) this.EndDate = Convert.ToDateTime(dr) [/code
2
2503
by: Arjen | last post by:
Hello, How can I check the recordcount? Here is a little bit of my code: // Load first row into Datareader dr.Read(); if (dr.ToString() == "xmlsrc") {
1
1325
by: Steve Caliendo | last post by:
Hi, I can't seem to figure out how to determine the number of records are returned when using a DataReader. I use: While Rs1.Read some stuff end while But what I'd really like to know is Rs1.RecordCount which doesn't exist. Or
20
5539
by: Mark | last post by:
Hi all, quick question , a DataView is memory resident "view" of data in a data table therefore once populated you can close the connection to the database. Garbage collection can then be used to "clean up" the DataView once it is not referenced and will not effect the number of connections to the database. A DataReader on the other hand always maintains a connection to the database and must be explicitly closed (Do not rely on garbage...
1
3092
by: Brent | last post by:
I'm having a hard time wrapping my head around how to build a multi-dimensional array of n length out of a DataReader loop. Take this pseudo-code: ======================================= public string get_array(string sql) { //create db connection & open
7
1662
by: bernadou | last post by:
How do I (or is this even possible) get the total number of records from a datasource control in ASP.NET 2.0? I’ve messed around with getting the row count from the grid that the datasource is bound to, but, that control uses paging and it appears that the count is only for the page that is in view. I’m guessing that this is pretty simple, but, I’m flummoxed. Thanks for any help you can offer.
0
9704
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9569
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
10558
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...
1
10302
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,...
1
7608
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
6844
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
5503
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
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3802
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.