PC Review


Reply
Thread Tools Rate Thread

datareader - how many records.

 
 
robert batt
Guest
Posts: n/a
 
      10th Oct 2003
Hello,
As you can see from the attached code I have a
datareader called Myreader. I wish to find out how many
records are in the datareader so that I can set the index
of the customers array precisely.



Dim customers(100) As Customer
i = 0

Do While (Myreader.Read()) And i <= 100
customers(i) = New Customer(Myreader
("customerkey").ToString, Myreader("companyname").ToString)
i = i + 1
Loop

Yours hopefully
Robert
 
Reply With Quote
 
 
 
 
Tim Stephenson
Guest
Posts: n/a
 
      10th Oct 2003
Data readers are not designed to provide access to the entire table. They
stream data from your database one record at a time.

To get a record count either run through all the records and count them,
then reset the datareader and read your data...
OR, use a dataadapter to fill a datatable object and use that instead of the
data reader.

--

Regards

Tim Stephenson MCSD.NET
Charted MCAD & MCSD.NET Early Achiever

"robert batt" <(E-Mail Removed)> wrote in message
news:1d8d01c38f32$3d199790$(E-Mail Removed)...
> Hello,
> As you can see from the attached code I have a
> datareader called Myreader. I wish to find out how many
> records are in the datareader so that I can set the index
> of the customers array precisely.
>
>
>
> Dim customers(100) As Customer
> i = 0
>
> Do While (Myreader.Read()) And i <= 100
> customers(i) = New Customer(Myreader
> ("customerkey").ToString, Myreader("companyname").ToString)
> i = i + 1
> Loop
>
> Yours hopefully
> Robert



 
Reply With Quote
 
Joshua Flanagan
Guest
Posts: n/a
 
      11th Oct 2003
You could add an additional query to your command object:

cmd.CommandText = "SELECT count(1) FROM customers;SELECT customerkey,
companyname FROM customers"

Then use the NextResult method of the reader: (note, I am coding on the fly,
not in an IDE - I dont guarantee the syntax is perfect)


MyReader = cmd.ExecuteReader()
myReader.Read()
customerCount = myReader.GetInt32(0)
Dim customers(customerCount) as Customer
myReader.NextResult
Do While (MyReader.Read() and i<=100)
....


OR, you could just use an ArrayList, in which case you dont need to know in
advance how many Customers there will be.

Dim custAL as ArrayList
custAL = new ArrayList()
Do While (MyReader.Read() and i<=100)
custAL.Add(new Customer(Myreader("customerkey").ToString,
Myreader("companyname").ToString))
i=i+1
Loop
Customers = custAl.ToArray( Customer.GetType() ) ' not sure how to do typeof
in VB

After you have added all of the customers from the datareader into the
ArrayList, use the ToArray method if you need it copied to a strongly typed
Customer array.



"robert batt" <(E-Mail Removed)> wrote in message
news:1d8d01c38f32$3d199790$(E-Mail Removed)...
> Hello,
> As you can see from the attached code I have a
> datareader called Myreader. I wish to find out how many
> records are in the datareader so that I can set the index
> of the customers array precisely.
>
>
>
> Dim customers(100) As Customer
> i = 0
>
> Do While (Myreader.Read()) And i <= 100
> customers(i) = New Customer(Myreader
> ("customerkey").ToString, Myreader("companyname").ToString)
> i = i + 1
> Loop
>
> Yours hopefully
> Robert



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
fetching records using datareader and updation nasirmajor@yahoo.com Microsoft ASP .NET 1 10th Jul 2006 01:35 PM
DataReader skips records?? =?Utf-8?B?TU4=?= Microsoft ADO .NET 7 23rd Nov 2004 04:51 PM
Number of records in DataReader Perre Van Wilrijk Microsoft ADO .NET 2 11th Sep 2004 02:23 AM
records returned from datareader error Davef Microsoft ASP .NET 1 28th Aug 2003 04:40 AM
Re: Can Locate records with datareader Kathleen Dollard Microsoft ADO .NET 0 6th Aug 2003 03:20 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:52 AM.