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. SqlDataReader Number of Rows

SqlDataReader Number of Rows

Scheduled Pinned Locked Moved Database
questiondatabase
7 Posts 3 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi, I've used an SqlDataReader to retrieve some rows from my database. I can then iterate through the results calling its Read() method but how can I determine the number of rows without having to call the Read() method? Thanks :)

    L T 2 Replies Last reply
    0
    • L Lost User

      Hi, I've used an SqlDataReader to retrieve some rows from my database. I can then iterate through the results calling its Read() method but how can I determine the number of rows without having to call the Read() method? Thanks :)

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

      Do a SELECT COUNT(*) that you read before entering the loop, or use a DataSet (with DataAdapter.Fill).

      I are troll :)

      L 1 Reply Last reply
      0
      • L Lost User

        Do a SELECT COUNT(*) that you read before entering the loop, or use a DataSet (with DataAdapter.Fill).

        I are troll :)

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

        Thanks for your reply :) I'm a bit of a SQL noob so I'll have to look into using a DataSet but doing a SELECT COUNT will be effectively calling the same query twice (apart from actually returning the columns). Won't that be a great deal of overhead? I just thought the SQLDataReader must know how many rows were returned when it's created from calling the SQLCommand's ExecuteReader() method. Is that the case?

        L P 2 Replies Last reply
        0
        • L Lost User

          Thanks for your reply :) I'm a bit of a SQL noob so I'll have to look into using a DataSet but doing a SELECT COUNT will be effectively calling the same query twice (apart from actually returning the columns). Won't that be a great deal of overhead? I just thought the SQLDataReader must know how many rows were returned when it's created from calling the SQLCommand's ExecuteReader() method. Is that the case?

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

          Member 6038196 wrote:

          Won't that be a great deal of overhead?

          A great deal? Not unless you're going to call this query often, couting the rows in a table and returning the result is quite fast.

          Member 6038196 wrote:

          I just thought the SQLDataReader must know how many rows were returned when it's created from calling the SQLCommand's ExecuteReader() method. Is that the case?

          If memory serves correct, it didn't - it just keeps getting records until it runs out of records. If you want both the records and the count in a single SQL-statement, use the DataAdapter.Fill method :)

          I are troll :)

          L 1 Reply Last reply
          0
          • L Lost User

            Member 6038196 wrote:

            Won't that be a great deal of overhead?

            A great deal? Not unless you're going to call this query often, couting the rows in a table and returning the result is quite fast.

            Member 6038196 wrote:

            I just thought the SQLDataReader must know how many rows were returned when it's created from calling the SQLCommand's ExecuteReader() method. Is that the case?

            If memory serves correct, it didn't - it just keeps getting records until it runs out of records. If you want both the records and the count in a single SQL-statement, use the DataAdapter.Fill method :)

            I are troll :)

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

            Thanks - much appreciated :)

            1 Reply Last reply
            0
            • L Lost User

              Thanks for your reply :) I'm a bit of a SQL noob so I'll have to look into using a DataSet but doing a SELECT COUNT will be effectively calling the same query twice (apart from actually returning the columns). Won't that be a great deal of overhead? I just thought the SQLDataReader must know how many rows were returned when it's created from calling the SQLCommand's ExecuteReader() method. Is that the case?

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              Member 6038196 wrote:

              I just thought the SQLDataReader must know how many rows were returned when it's created from calling the SQLCommand's ExecuteReader() method. Is that the case?

              A datareader only ever knows about 1 row. It's what's known as a Firehose cursor, whereby data is squirted through as quickly as possible. You could always just increment a count on each read and use that.

              "WPF has many lovers. It's a veritable porn star!" - Josh Smith

              As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

              My blog | My articles | MoXAML PowerToys

              1 Reply Last reply
              0
              • L Lost User

                Hi, I've used an SqlDataReader to retrieve some rows from my database. I can then iterate through the results calling its Read() method but how can I determine the number of rows without having to call the Read() method? Thanks :)

                T Offline
                T Offline
                tech603
                wrote on last edited by
                #7

                Another solution for you if the others do not work, SqlDataReader reader = cmd.ExcecuteReader(); int count = 0; while ( reader.read){ // do your normal code for gathering column info count++; } when its done reading you'll have your data and your count. Hope that helps.

                Matthew Vass QA Analyst mvass@hostmysite.com HostMySite.com

                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