datareader - how many records.

  • Thread starter Thread starter robert batt
  • Start date Start date
R

robert batt

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top