Simple question

  • Thread starter Thread starter Gary
  • Start date Start date
G

Gary

Hi,

I have a question is regarding OLEDbDataReader

Is there a way to access Data filled to an OLEDBDataReader using field name
instead of field position.

If not possible using OLEDbDataReader, is there any alternative to achieve
this?

TIA,

Gary
 
Gary said:
I have a question is regarding OLEDbDataReader
Is there a way to access Data filled to an OLEDBDataReader using field name
instead of field position.
Declare variables and store the position of selected fields before you start
the read loop :
VB.NET:
Dim iFirstnamePosition As Integer = myReader.GetOrdinal("first_name")
Dim iLastnamePosition As Integer = myReader.GetOrdinal("last_name")
Do While myReader.Read()
Console.WriteLine("Full name: {0} {1}",
myReader.GetString(iFirstNamePosition),
myReader.GetString(iLastNamePosition))
Loop

C#:
int iFirstNamePosition = myReader.GetOrdinal("first_name");
int iLastNamePosition = myReader.GetOrdinal("last_name");
while (myReaderRead()) {
Console.WriteLine("Full name: {0} {1}",
myReader.GetString(iFirstNamePosition),
myReader.GetString(iLastNamePosition));
}

The xxxDataReader is supposed to be very performant, you would have a
performance decrease if you lookup a field name every time you fetch a new
row. Working with column ordinals is the fastest method.
 
Back
Top