Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DbDataReader: Matching fields to order or occurrence?

DbDataReader: Matching fields to order or occurrence?

Scheduled Pinned Locked Moved C#
question
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Goalie35
    wrote on last edited by
    #1

    I have to read an excel file and ensure the columns are in their correct order (the file is created manually and sometimes, columns are inserted in the incorrect order). I'm reading the file via microsoft jet oledb 4.0. I then insert the values into a DbDataReader. I now have to check to see if each column is in it's propper position. So, as of now, I have the following to start with (The data reader field names are based on the column header names of the excel file):

    using (DbDataReader dr = command.ExecuteReader())
    {
    while (dr.Read())
    {
    String col1 = dr["OrderId"];
    }
    }

    But my question is, how do I ensure dr["OrderId"] is the 1st field within the dbDataReader? Thanks

    D E P 3 Replies Last reply
    0
    • G Goalie35

      I have to read an excel file and ensure the columns are in their correct order (the file is created manually and sometimes, columns are inserted in the incorrect order). I'm reading the file via microsoft jet oledb 4.0. I then insert the values into a DbDataReader. I now have to check to see if each column is in it's propper position. So, as of now, I have the following to start with (The data reader field names are based on the column header names of the excel file):

      using (DbDataReader dr = command.ExecuteReader())
      {
      while (dr.Read())
      {
      String col1 = dr["OrderId"];
      }
      }

      But my question is, how do I ensure dr["OrderId"] is the 1st field within the dbDataReader? Thanks

      D Offline
      D Offline
      dasblinkenlight
      wrote on last edited by
      #2

      You can try the other overload[^] of the DbDataReader's Item[] property:

      using (DbDataReader dr = command.ExecuteReader())
      {
      while (dr.Read())
      {
      String col1 = dr[1];
      }
      }

      1 Reply Last reply
      0
      • G Goalie35

        I have to read an excel file and ensure the columns are in their correct order (the file is created manually and sometimes, columns are inserted in the incorrect order). I'm reading the file via microsoft jet oledb 4.0. I then insert the values into a DbDataReader. I now have to check to see if each column is in it's propper position. So, as of now, I have the following to start with (The data reader field names are based on the column header names of the excel file):

        using (DbDataReader dr = command.ExecuteReader())
        {
        while (dr.Read())
        {
        String col1 = dr["OrderId"];
        }
        }

        But my question is, how do I ensure dr["OrderId"] is the 1st field within the dbDataReader? Thanks

        E Offline
        E Offline
        Ennis Ray Lynch Jr
        wrote on last edited by
        #3

        dr.GetOrdinal("fieldName")

        Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

        1 Reply Last reply
        0
        • G Goalie35

          I have to read an excel file and ensure the columns are in their correct order (the file is created manually and sometimes, columns are inserted in the incorrect order). I'm reading the file via microsoft jet oledb 4.0. I then insert the values into a DbDataReader. I now have to check to see if each column is in it's propper position. So, as of now, I have the following to start with (The data reader field names are based on the column header names of the excel file):

          using (DbDataReader dr = command.ExecuteReader())
          {
          while (dr.Read())
          {
          String col1 = dr["OrderId"];
          }
          }

          But my question is, how do I ensure dr["OrderId"] is the 1st field within the dbDataReader? Thanks

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          There should never be any reason to insist that the columns are in a particular order. You're doing something wrong.

          L 1 Reply Last reply
          0
          • P PIEBALDconsult

            There should never be any reason to insist that the columns are in a particular order. You're doing something wrong.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            +5, since the order of the columns in a record could be coming from the server in a random order. And life is soo much easier if you just decide the order in the Presentation Layer - preferably having the user choose one. OTOH, this is an Excel-sheet, and it might come without headers. In that case, only the position of the column would indicate what cell (dbfield) you're actually editing. Things become harder if the user is allowed to move those things around. In an ideal world, each row would have a "caption" on top of the file.

            Bastard Programmer from Hell :suss:

            P 1 Reply Last reply
            0
            • L Lost User

              +5, since the order of the columns in a record could be coming from the server in a random order. And life is soo much easier if you just decide the order in the Presentation Layer - preferably having the user choose one. OTOH, this is an Excel-sheet, and it might come without headers. In that case, only the position of the column would indicate what cell (dbfield) you're actually editing. Things become harder if the user is allowed to move those things around. In an ideal world, each row would have a "caption" on top of the file.

              Bastard Programmer from Hell :suss:

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              Eddy Vluggen wrote:

              it might come without headers

              Then you're hosed. :-D

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups