Iterate through a recordset

S

Steve Schroeder

I'm iterating through a recordset using:

Do While InvoiceDataReader.Read

Loop

What can I put between the loop to just see the values of a field as the
loop loops?

Debug.Print don't work, console.write doesn't either...

Thanks
 
W

W.G. Ryan eMVP

InvoiceReader.GetValue(Index) , GetString(Index), GetInt32 - you can use the
index corresponding to the field's positino in the query Select Firstname,
Lastname would yeild Firstname with an index of 0, Lastname as 1. You can
also ust the name, GetString("FirstName") but this is painfully inefficient
b/c you need a lookup each time - so you can use GetOrdinal instead.
 
S

Steve Schroeder

How's SqlDataReader("FieldName")

Seems to work, is it recommended?

I was really looking for the equivelent of Debug.Print or some such thing. I
managed to kludge together a textbox on the ASP page so I can 'see' my
values as I loop, but would rather do something like Debug.Print...thanks
for the pointers :)
 
W

W.G. Ryan eMVP

Turn on OPtion Strict ;-). You can use SqlDataReader(FieldName) but it's
the slowest way to access data.

At the top of your class, add Imports System.Diagnostics (assuming you're in
VB.NET) or using System.Diagnostics in C#. Your Debug will work fine them.
 
S

Sahil Malik [MVP]

In addition to what Bill is recommending, as a good design practise I'd want
to recommend that use the firehose cursor DataReader as - somethign that you
intend to spurt out the data, and close it ASAP - in other words, within the
loop, don't do any heavy duty processing or your concurrent application
performance will nosedive.

Regards debugging values in an ASP or ASP.NET page, just do
Response.Write(DataReader["columnname"].ToString()) <-- That should do the
trick .. if you don't want to see the output on the page, you can always do
System.Diagnostics.Debug.WriteLine.

- Sahil Malik [MVP]
http://codebetter.com/blogs/sahil.malik/
 
S

Steve Schroeder

Making progress here...

Ok, so I turn on Option Strict which forces me to explicitly get a string,
integer, whatever value from the DataReader, as you show below.

This means I can't use the field name as a reference, right, I must use the
ordinal positional value. I have two issues with that, and tell me if I'm
off base here.

1. Ordinal values are not self-documenting, i.e.
InvoiceDataReader.GetString(0) tells me only that I'm pulling from the first
field. InvoiceDataReader("Invoice") tells me much more. Especially 6 months
down the line when I haven't looked at the code for awhile...

2. Were the order of columns, or additional columns inserted, due to
buisness needs, I would have to adjust all or some of my ordinal references
to match the updated column order.

Certainly #1 is easily resolved by commenting my code :) i.e.
InvoiceDataReader.GetString(0) '<----Invoice Number

Nevertheless...do I have a point? I'm not discounting anyone's advice
certainly. Just discussing the merits. Are we talking fractions of a second
in difference of performance?

Thanks
 
W

W.G. Ryan eMVP

Steve- you have a point. You can use an Enumeration (Bill Vaugh's
suggestion) so you get intellisense support + the benefit of using an
integer - or before you loop you can use Int32 Whatever =
dr.GetOrdinal["Invoice"]; then you can use Whatever in the
GetString(Whatever);
 
S

Steve Schroeder

I see a Bill Ryan, but no Vaugh...

<rub eyes>

Nope, still don't see him...Bill?

Thanks however...seems like I have a few options...

W.G. Ryan eMVP said:
Steve- you have a point. You can use an Enumeration (Bill Vaugh's
suggestion) so you get intellisense support + the benefit of using an
integer - or before you loop you can use Int32 Whatever =
dr.GetOrdinal["Invoice"]; then you can use Whatever in the
GetString(Whatever);

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
Steve Schroeder said:
Making progress here...

Ok, so I turn on Option Strict which forces me to explicitly get a string,
integer, whatever value from the DataReader, as you show below.

This means I can't use the field name as a reference, right, I must use the
ordinal positional value. I have two issues with that, and tell me if I'm
off base here.

1. Ordinal values are not self-documenting, i.e.
InvoiceDataReader.GetString(0) tells me only that I'm pulling from the first
field. InvoiceDataReader("Invoice") tells me much more. Especially 6 months
down the line when I haven't looked at the code for awhile...

2. Were the order of columns, or additional columns inserted, due to
buisness needs, I would have to adjust all or some of my ordinal references
to match the updated column order.

Certainly #1 is easily resolved by commenting my code :) i.e.
InvoiceDataReader.GetString(0) '<----Invoice Number

Nevertheless...do I have a point? I'm not discounting anyone's advice
certainly. Just discussing the merits. Are we talking fractions of a second
in difference of performance?

Thanks

use
the
 
S

Steve Schroeder

This allows me to refer to the columns by name rather than ordinal, and
still use Option Strict? By using and populating a temporary DataTable, and
using GetValues? I appreciate the suggestion and advice, thanks :)

Steve
 

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

Top