Accessing Datareader Columns using column names

M

mavrick_101

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

Andy O'Neill

mavrick_101 said:
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.
 
S

sloan

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/library/system.data.idatareader.aspx
http://msdn.microsoft.com/en-us/library/system.data.idatarecord.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...tation&aq=f&aqi=&aql=&oq=&fp=c26c79a56c95bda8

One Hit:
http://www.artima.com/lejava/articles/designprinciples.html
 
M

mavrick_101

Thanks guys,

Your reason makes sense.

Andy O'Neill said:
mavrick_101 said:
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.


.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top