Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - DataReader and RecordCount

 
Thread Tools Search this Thread
Old 10-21-2005, 03:53 PM   #1
Default 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?


=?Utf-8?B?UGF1bA==?=
  Reply With Quote
Old 10-21-2005, 04:23 PM   #2
Mark Rae
 
Posts: n/a
Default Re: DataReader and RecordCount
"Paul" <> wrote in message
news:7AD0ED33-BB76-4D13-8BBD-...

> 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...




Mark Rae
  Reply With Quote
Old 10-21-2005, 04:30 PM   #3
¿ Mahesh Kumar
 
Posts: n/a
Default Re: DataReader and RecordCount
declare 1 integer and assign the Datareader.ExecuteReader();
This method will return some integer(how many records affected / inserted) ,
work on that.
int i=Datareader.ExecuteReader(); --try ?

Mahesh
www.snipurl.com/guac

"Paul" <> wrote in message
news:7AD0ED33-BB76-4D13-8BBD-...
> 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?





¿ Mahesh Kumar
  Reply With Quote
Old 10-21-2005, 05:54 PM   #4
=?Utf-8?B?UGF1bA==?=
 
Posts: n/a
Default Re: DataReader and RecordCount
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.ExecuteReader();
> This method will return some integer(how many records affected / inserted) ,
> work on that.
> int i=Datareader.ExecuteReader(); --try ?
>
> Mahesh
> www.snipurl.com/guac
>
> "Paul" <> wrote in message
> news:7AD0ED33-BB76-4D13-8BBD-...
> > 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?

>
>
>



=?Utf-8?B?UGF1bA==?=
  Reply With Quote
Old 10-21-2005, 06:01 PM   #5
=?Utf-8?B?UGF1bA==?=
 
Posts: n/a
Default Re: DataReader and RecordCount
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" <> wrote in message
> news:7AD0ED33-BB76-4D13-8BBD-...
>
> > 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...
>
>
>



=?Utf-8?B?UGF1bA==?=
  Reply With Quote
Old 10-21-2005, 06:04 PM   #6
Mark Rae
 
Posts: n/a
Default Re: DataReader and RecordCount
"Paul" <> wrote in message
news:B008E19F-FAD6-44FB-B8B5-...

> 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




Mark Rae
  Reply With Quote
Old 10-21-2005, 06:07 PM   #7
Mark Rae
 
Posts: n/a
Default Re: DataReader and RecordCount
"¿ Mahesh Kumar" <> wrote in message
news:u%...

> declare 1 integer and assign the Datareader.ExecuteReader();
> This method will return some integer(how many records affected / inserted)
> ,
> work on that.
> int i=Datareader.ExecuteReader(); --try ?


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

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




Mark Rae
  Reply With Quote
Old 10-21-2005, 06:08 PM   #8
Mark Rae
 
Posts: n/a
Default Re: DataReader and RecordCount
"Paul" <> wrote in message
news:922F8088-128F-4AB6-A999-...

> 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.




Mark Rae
  Reply With Quote
Old 10-21-2005, 06:09 PM   #9
Mark Rae
 
Posts: n/a
Default Re: DataReader and RecordCount
"Mark Rae" <> wrote in message
news:...

> <DataSet object>.Tables[0].Rows[0].Count


Woops, that should be

DataSet object>.Tables[0].Rows.Count




Mark Rae
  Reply With Quote
Old 10-22-2005, 11:46 AM   #10
Patrick.O.Ige
 
Posts: n/a
Default Re: DataReader and RecordCount
There a many ways to do that.
You can make use of "ExecuteScalar "
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=counter

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

"Mark Rae" <> wrote in message
news:##5p#...
> "¿ Mahesh Kumar" <> wrote in message
> news:u%...
>
> > declare 1 integer and assign the Datareader.ExecuteReader();
> > This method will return some integer(how many records affected /

inserted)
> > ,
> > work on that.
> > int i=Datareader.ExecuteReader(); --try ?

>
> 1) ExecuteReader() is a method of the Command object, not the DataReader
> object.
>
> 2) ExecuteReader() returns a DataReader object, not an integer.
>
>





Patrick.O.Ige
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46