Accessing Datareader Columns using column names

Discussion in 'ASP .Net' started by mavrick_101, Feb 24, 2010.

  1. mavrick_101

    mavrick_101 Guest

    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...
     
    mavrick_101, Feb 24, 2010
    #1
    1. Advertising

  2. mavrick_101

    Andy O'Neill Guest

    "mavrick_101" <> wrote in message
    news:...
    > 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.
     
    Andy O'Neill, Feb 24, 2010
    #2
    1. Advertising

  3. mavrick_101

    sloan Guest

    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





    "mavrick_101" <> wrote in message
    news:...
    > 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...
    >
     
    sloan, Feb 24, 2010
    #3
  4. mavrick_101

    mavrick_101 Guest

    Thanks guys,

    Your reason makes sense.

    "Andy O'Neill" wrote:

    >
    > "mavrick_101" <> wrote in message
    > news:...
    > > 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.
    >
    >
    > .
    >
     
    mavrick_101, Feb 25, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. brian shannon

    datareader columns

    brian shannon, Jan 22, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,053
    Teemu Keiski
    Jan 23, 2004
  2. Bigtoga
    Replies:
    1
    Views:
    676
    Chee Seong Ong
    Apr 28, 2004
  3. Mattyw
    Replies:
    2
    Views:
    2,942
    Mattyw
    Sep 12, 2005
  4. Carl
    Replies:
    0
    Views:
    610
  5. Ravichandran Mahalingam

    DG-Edit-Loosing data; bound columns - pushbuttons; dataReader

    Ravichandran Mahalingam, Jan 8, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    161
    Ravichandran Mahalingam
    Jan 8, 2004
Loading...

Share This Page