Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Accessing Datareader Columns using column names

Reply
Thread Tools

Accessing Datareader Columns using column names

 
 
mavrick_101
Guest
Posts: n/a
 
      02-24-2010
Hi,

I recently wrote some code and used column names to access values in the
datareader, like ....

string _firstName = myDr["FirstName"].ToString();

My supervisor instructed me to use index instead, like...

string _firstName = myDr.getString(0);

I wouldn't mind using index but whats the big deal? I can't understand why
is one better than the other?

Please help...

 
Reply With Quote
 
 
 
 
Andy O'Neill
Guest
Posts: n/a
 
      02-24-2010

"mavrick_101" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> I recently wrote some code and used column names to access values in the
> datareader, like ....
>
> string _firstName = myDr["FirstName"].ToString();
>
> My supervisor instructed me to use index instead, like...
>
> string _firstName = myDr.getString(0);
>
> I wouldn't mind using index but whats the big deal? I can't understand why
> is one better than the other?
>
> Please help...
>


It's more efficient.
Each time you use the name it has to go find the index for that name so
using the index misses that bit out.
It's only really going to be significant if you read a load of data and
readability suffers unless you first use getordinal.


 
Reply With Quote
 
 
 
 
sloan
Guest
Posts: n/a
 
      02-24-2010
It avoids an unnecessary lookup. So (for once anyways (haha)) your
supervisor is "more correct".

And you might want to use the methods defined by the IDataReader interface,
instead of the particuliar concrete you happen to be using.
http://msdn.microsoft.com/en-us/libr...atareader.aspx
http://msdn.microsoft.com/en-us/libr...getstring.aspx
(Notice that .GetString on the interface only takes an int) ***

However, I find .GetString(0) less readable and less maintainable. When you
have a "0" "1", "2", ........"18" in there, its hard to "see" what is going
on.
But do NOT take that as I prefer dr["SomeString"], because I do not.

For an idea that avoids unnecessary lookups AND promotes
readability......(and maintenance), look here:
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!140.entry

Download the code, and pay attention to the definitions and usage of
CustomerLayout(.cs) and OrderLayout(.cs)
There are comments in each of those files explaining their usage.



***
If you're not sure why I said this, then google or bing this:
http://www.google.com/#hl=en&source=...6c79a56c95bda8

One Hit:
http://www.artima.com/lejava/article...rinciples.html





"mavrick_101" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> I recently wrote some code and used column names to access values in the
> datareader, like ....
>
> string _firstName = myDr["FirstName"].ToString();
>
> My supervisor instructed me to use index instead, like...
>
> string _firstName = myDr.getString(0);
>
> I wouldn't mind using index but whats the big deal? I can't understand why
> is one better than the other?
>
> Please help...
>



 
Reply With Quote
 
mavrick_101
Guest
Posts: n/a
 
      02-25-2010
Thanks guys,

Your reason makes sense.

"Andy O'Neill" wrote:

>
> "mavrick_101" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Hi,
> >
> > I recently wrote some code and used column names to access values in the
> > datareader, like ....
> >
> > string _firstName = myDr["FirstName"].ToString();
> >
> > My supervisor instructed me to use index instead, like...
> >
> > string _firstName = myDr.getString(0);
> >
> > I wouldn't mind using index but whats the big deal? I can't understand why
> > is one better than the other?
> >
> > Please help...
> >

>
> It's more efficient.
> Each time you use the name it has to go find the index for that name so
> using the index misses that bit out.
> It's only really going to be significant if you read a load of data and
> readability suffers unless you first use getordinal.
>
>
> .
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Reading all the Rows in a Column using DataReader Mattyw ASP .Net 2 09-12-2005 08:55 AM
CheckBox Bound to DataReader column inside DataList Bigtoga ASP .Net 1 04-28-2004 06:16 PM
Match attribute values (columns) to column names Carl XML 0 04-04-2004 05:12 PM
datareader columns brian shannon ASP .Net 3 01-23-2004 04:28 PM
DG-Edit-Loosing data; bound columns - pushbuttons; dataReader Ravichandran Mahalingam ASP .Net Datagrid Control 0 01-08-2004 10:28 PM



Advertisments