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. Database & SysAdmin
  3. Database
  4. How to get record count from OledbDataReader?

How to get record count from OledbDataReader?

Scheduled Pinned Locked Moved Database
tutorialquestion
3 Posts 2 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.
  • R Offline
    R Offline
    rushing
    wrote on last edited by
    #1

    I have get some records by OleDbDataReader like this, OledbDataReader reader=cmd.ExecuteReader(); How to get the amount of records from it? Thanks!

    C 1 Reply Last reply
    0
    • R rushing

      I have get some records by OleDbDataReader like this, OledbDataReader reader=cmd.ExecuteReader(); How to get the amount of records from it? Thanks!

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      rushing wrote:

      How to get the amount of records from it?

      Read all the records from it counting them as you go. There is no other way with a DataReader. But, you do have the option of of getting the database to tell you in advance (assuming you need to know in advance) by sending off a query to find out how many rows it expects to return. You can do this in the same command as your main data, or you can use a separate command. If it is the former you can do something like this:

      OleDbCommand cmd = new SqlCommand();
      cmd.Connection = myConnection;
      cmd.CommandText = "SELECT COUNT(*) FROM MyTable; SELECT * FROM MyTable";
      OleDbDataReader reader = cmd.ExecuteReader();
      reader.Read(); // Reads the next row
      int numRows = reader.GetInt();
      reader.NextResult(); // Moves to the next result set in the reader
      while(reader.Read()) // Reads the next row, returns false if there isn't another row
      {
      // Do stuff.
      }

      Or you can use a separate command object requesting the count of the rows and use ExecuteScalar() on it. Does this help? ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

      R 1 Reply Last reply
      0
      • C Colin Angus Mackay

        rushing wrote:

        How to get the amount of records from it?

        Read all the records from it counting them as you go. There is no other way with a DataReader. But, you do have the option of of getting the database to tell you in advance (assuming you need to know in advance) by sending off a query to find out how many rows it expects to return. You can do this in the same command as your main data, or you can use a separate command. If it is the former you can do something like this:

        OleDbCommand cmd = new SqlCommand();
        cmd.Connection = myConnection;
        cmd.CommandText = "SELECT COUNT(*) FROM MyTable; SELECT * FROM MyTable";
        OleDbDataReader reader = cmd.ExecuteReader();
        reader.Read(); // Reads the next row
        int numRows = reader.GetInt();
        reader.NextResult(); // Moves to the next result set in the reader
        while(reader.Read()) // Reads the next row, returns false if there isn't another row
        {
        // Do stuff.
        }

        Or you can use a separate command object requesting the count of the rows and use ExecuteScalar() on it. Does this help? ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

        R Offline
        R Offline
        rushing
        wrote on last edited by
        #3

        Thanks. However, this sql statement does not work in Oracle. It is seem that we could not write two sql statement in one line.

        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