DataReader

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lakshmi

    DataReader

    Hi I'm using a SqlDataReader to read results from a SQL
    view that returns 10 columns.
    Is there anyway I can reader the columns based on their
    names rather than their position?

    strQuery = "SELECT customerid, orderno from orders where
    orderID = 1";
    SqlDataReader reader = cmd.ExecuteRead er();
    while(reader.Re ad())
    {
    myOrderNo = reader.GetInt32 (1);
    }

    instead of using reader.GetInt32 (1), is there any syntax
    that will support reader("OrderNo ") or something like that?

    Thanks for your help.

  • Ignacio Machin

    #2
    Re: DataReader

    Hi,

    I do it all the time !

    Did you tried it?

    Just a comment, remember that you are getting the Item property in this case
    that is the indexer , therefore you have to write
    reader["OrderNo"] using [] instead of ()

    Hope this help,

    --
    Ignacio Machin,
    ignacio.machin AT dot.state.fl.us
    Florida Department Of Transportation

    "lakshmi" <lm@replytonews group.net> wrote in message
    news:039101c37e d2$e32c9e90$a30 1280a@phx.gbl.. .[color=blue]
    > Hi I'm using a SqlDataReader to read results from a SQL
    > view that returns 10 columns.
    > Is there anyway I can reader the columns based on their
    > names rather than their position?
    >
    > strQuery = "SELECT customerid, orderno from orders where
    > orderID = 1";
    > SqlDataReader reader = cmd.ExecuteRead er();
    > while(reader.Re ad())
    > {
    > myOrderNo = reader.GetInt32 (1);
    > }
    >
    > instead of using reader.GetInt32 (1), is there any syntax
    > that will support reader("OrderNo ") or something like that?
    >
    > Thanks for your help.
    >[/color]


    Comment

    • lakshmi

      #3
      Re: DataReader

      thanks a lot Ignacio. It worked.[color=blue]
      >-----Original Message-----
      >Hi,
      >
      > I do it all the time !
      >
      >Did you tried it?
      >
      >Just a comment, remember that you are getting the Item[/color]
      property in this case[color=blue]
      >that is the indexer , therefore you have to write
      >reader["OrderNo"] using [] instead of ()
      >
      >Hope this help,
      >
      >--
      >Ignacio Machin,
      >ignacio.mach in AT dot.state.fl.us
      >Florida Department Of Transportation
      >
      >"lakshmi" <lm@replytonews group.net> wrote in message
      >news:039101c37 ed2$e32c9e90$a3 01280a@phx.gbl. ..[color=green]
      >> Hi I'm using a SqlDataReader to read results from a SQL
      >> view that returns 10 columns.
      >> Is there anyway I can reader the columns based on their
      >> names rather than their position?
      >>
      >> strQuery = "SELECT customerid, orderno from orders where
      >> orderID = 1";
      >> SqlDataReader reader = cmd.ExecuteRead er();
      >> while(reader.Re ad())
      >> {
      >> myOrderNo = reader.GetInt32 (1);
      >> }
      >>
      >> instead of using reader.GetInt32 (1), is there any syntax
      >> that will support reader("OrderNo ") or something like[/color][/color]
      that?[color=blue][color=green]
      >>
      >> Thanks for your help.
      >>[/color]
      >
      >
      >.
      >[/color]

      Comment

      • William Ryan

        #4
        Re: DataReader

        You can use the GetOrdinal property and assign it to a string. This way you
        get the performance of the Index based approach with the readability of the
        Literal name. like this...

        DIM fieldCityPos as integer=reader1 .getordinal("ci ty")


        Using the column slows things down because the column index needs to be
        resolved each time a row is read...

        HTH,

        Bill
        "lakshmi" <lm@newsgroup.n et> wrote in message
        news:899301c37e d7$3cad88e0$a60 1280a@phx.gbl.. .[color=blue]
        > thanks a lot Ignacio. It worked.[color=green]
        > >-----Original Message-----
        > >Hi,
        > >
        > > I do it all the time !
        > >
        > >Did you tried it?
        > >
        > >Just a comment, remember that you are getting the Item[/color]
        > property in this case[color=green]
        > >that is the indexer , therefore you have to write
        > >reader["OrderNo"] using [] instead of ()
        > >
        > >Hope this help,
        > >
        > >--
        > >Ignacio Machin,
        > >ignacio.mach in AT dot.state.fl.us
        > >Florida Department Of Transportation
        > >
        > >"lakshmi" <lm@replytonews group.net> wrote in message
        > >news:039101c37 ed2$e32c9e90$a3 01280a@phx.gbl. ..[color=darkred]
        > >> Hi I'm using a SqlDataReader to read results from a SQL
        > >> view that returns 10 columns.
        > >> Is there anyway I can reader the columns based on their
        > >> names rather than their position?
        > >>
        > >> strQuery = "SELECT customerid, orderno from orders where
        > >> orderID = 1";
        > >> SqlDataReader reader = cmd.ExecuteRead er();
        > >> while(reader.Re ad())
        > >> {
        > >> myOrderNo = reader.GetInt32 (1);
        > >> }
        > >>
        > >> instead of using reader.GetInt32 (1), is there any syntax
        > >> that will support reader("OrderNo ") or something like[/color][/color]
        > that?[color=green][color=darkred]
        > >>
        > >> Thanks for your help.
        > >>[/color]
        > >
        > >
        > >.
        > >[/color][/color]


        Comment

        Working...