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. Getting the row count from a SQLDataReader

Getting the row count from a SQLDataReader

Scheduled Pinned Locked Moved Database
csharpdatabasecomdata-structurestutorial
13 Posts 4 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.
  • D Daniel Turini

    You can't, because it's a forward-only cursor. Paul Watson wrote: But now I have just realised I cannot figure out how to get a row count from the reader without actually looping through it, which bites because I need the rowcount before I loop so that I can initialise a collection array. Doh. You can create an ArrayList and, at the end, use the ArrayList.CopyTo method for copying it to typed array. Since this will only copy object references, not the actual values, it's very fast. Normally much, much faster than a SQL count. Q261186 - Computer Randomly Plays Classical Music

    P Offline
    P Offline
    Paul Watson
    wrote on last edited by
    #4

    Daniel Turini wrote: You can't, because it's a forward-only cursor. I know, was hoping someone had some trick up their sleave :) Daniel Turini wrote: Since this will only copy object references, not the actual values, it's very fast. Normally much, much faster than a SQL count. Great idea, thanks Daniel.

    Paul Watson
    Bluegrass
    Cape Town, South Africa

    1 Reply Last reply
    0
    • D Daniel Turini

      You can't, because it's a forward-only cursor. Paul Watson wrote: But now I have just realised I cannot figure out how to get a row count from the reader without actually looping through it, which bites because I need the rowcount before I loop so that I can initialise a collection array. Doh. You can create an ArrayList and, at the end, use the ArrayList.CopyTo method for copying it to typed array. Since this will only copy object references, not the actual values, it's very fast. Normally much, much faster than a SQL count. Q261186 - Computer Randomly Plays Classical Music

      M Offline
      M Offline
      Morten Abrahamsen
      wrote on last edited by
      #5

      If you had used an SQL COUNT() function before you retrieved the it probably wouldn't be accurate either (atleast not in a multiuser enviornment), unless it's run in a serializable transaction. The SqlDataReader streams the data and will reflect all the changes in the database until the data has passed through the reader. So records could easily be added or deleted after the count was issued and while the reader is processing. :) Morty

      D 1 Reply Last reply
      0
      • M Morten Abrahamsen

        If you had used an SQL COUNT() function before you retrieved the it probably wouldn't be accurate either (atleast not in a multiuser enviornment), unless it's run in a serializable transaction. The SqlDataReader streams the data and will reflect all the changes in the database until the data has passed through the reader. So records could easily be added or deleted after the count was issued and while the reader is processing. :) Morty

        D Offline
        D Offline
        Daniel Turini
        wrote on last edited by
        #6

        Morten Abrahamsen wrote: If you had used an SQL COUNT() function before you retrieved the it probably wouldn't be accurate either (atleast not in a multiuser enviornment), unless it's run in a serializable transaction. Nice point Morten Abrahamsen wrote: The SqlDataReader streams the data and will reflect all the changes in the database until the data has passed through the reader. So records could easily be added or deleted after the count was issued and while the reader is processing. This is not true: The .NET framework does not provides support for server-side cursors, only client-side cursors. This way, only deleted records would be noticed by SqlDataReader. Q261186 - Computer Randomly Plays Classical Music

        M 1 Reply Last reply
        0
        • D Daniel Turini

          Morten Abrahamsen wrote: If you had used an SQL COUNT() function before you retrieved the it probably wouldn't be accurate either (atleast not in a multiuser enviornment), unless it's run in a serializable transaction. Nice point Morten Abrahamsen wrote: The SqlDataReader streams the data and will reflect all the changes in the database until the data has passed through the reader. So records could easily be added or deleted after the count was issued and while the reader is processing. This is not true: The .NET framework does not provides support for server-side cursors, only client-side cursors. This way, only deleted records would be noticed by SqlDataReader. Q261186 - Computer Randomly Plays Classical Music

          M Offline
          M Offline
          Morten Abrahamsen
          wrote on last edited by
          #7

          What do you base this on ? AFAIK, the data is streamed directly from the server, and even though it doesn't utilize server-side cursors it doesn't precalculate the entire dataset (it's streamed ... standard SQL Server dataset processing...). So if records are appended during the read it should be reflected. However I could be wrong, so if you have any docs / references please post them :) .NET SDK: Changes made to a resultset by another process or thread while data is being read may be visible to the user of the SqlDataReader. However, the precise behavior is timing dependent. Morty

          D 1 Reply Last reply
          0
          • M Morten Abrahamsen

            What do you base this on ? AFAIK, the data is streamed directly from the server, and even though it doesn't utilize server-side cursors it doesn't precalculate the entire dataset (it's streamed ... standard SQL Server dataset processing...). So if records are appended during the read it should be reflected. However I could be wrong, so if you have any docs / references please post them :) .NET SDK: Changes made to a resultset by another process or thread while data is being read may be visible to the user of the SqlDataReader. However, the precise behavior is timing dependent. Morty

            D Offline
            D Offline
            Daniel Turini
            wrote on last edited by
            #8

            You may be right: I'm sure the SqlDataReader is a client-side cursor (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsent7/html/dvconChoosingRightDataAccessTechnology.asp[^]) but the rest was a conclusion of mine... Q261186 - Computer Randomly Plays Classical Music

            M 1 Reply Last reply
            0
            • D Daniel Turini

              You may be right: I'm sure the SqlDataReader is a client-side cursor (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsent7/html/dvconChoosingRightDataAccessTechnology.asp[^]) but the rest was a conclusion of mine... Q261186 - Computer Randomly Plays Classical Music

              M Offline
              M Offline
              Morten Abrahamsen
              wrote on last edited by
              #9

              Well... the SqlDataReader is by no means a client side cursor... it's simply a TDS parser. That's what the dataset is for ;) Would be cool to check out the theory though... (how much locking does the TDS generator really support...) but as usual, I would probably never find the time. Morty

              D 1 Reply Last reply
              0
              • M Morten Abrahamsen

                Well... the SqlDataReader is by no means a client side cursor... it's simply a TDS parser. That's what the dataset is for ;) Would be cool to check out the theory though... (how much locking does the TDS generator really support...) but as usual, I would probably never find the time. Morty

                D Offline
                D Offline
                Daniel Turini
                wrote on last edited by
                #10

                Morten Abrahamsen wrote: Well... the SqlDataReader is by no means a client side cursor... it's simply a TDS parser. That's what the dataset is for Well, if it is a TDS parser it should be pretty fast! I think you're right, but some Anakrino hacking may solve this issue :) Q261186 - Computer Randomly Plays Classical Music

                M 1 Reply Last reply
                0
                • D Daniel Turini

                  Morten Abrahamsen wrote: Well... the SqlDataReader is by no means a client side cursor... it's simply a TDS parser. That's what the dataset is for Well, if it is a TDS parser it should be pretty fast! I think you're right, but some Anakrino hacking may solve this issue :) Q261186 - Computer Randomly Plays Classical Music

                  M Offline
                  M Offline
                  Morten Abrahamsen
                  wrote on last edited by
                  #11

                  Hehe... been there ;) However, this is not an issue dependant on the .NET SQL Client layer. It's a question of how the SQL Server handles standard query processing. If you issue a select query with a 100.000 record resultset, it would be returned as a datastream (TDS) to the client. I would think that if there is no isolation (tx) there would just be a read lock on the current row (or index key) and not on the table, so records could easily be added or deleted. So if I'm correct the SQL Server would just read the data page by page and never precalculate the total amount and lock it. Hence the "imagined resultset" could be radically (add/delete) changed during the processing (which could take time), and a prior Count would be useless. Just my 2c :)

                  D 1 Reply Last reply
                  0
                  • M Morten Abrahamsen

                    Hehe... been there ;) However, this is not an issue dependant on the .NET SQL Client layer. It's a question of how the SQL Server handles standard query processing. If you issue a select query with a 100.000 record resultset, it would be returned as a datastream (TDS) to the client. I would think that if there is no isolation (tx) there would just be a read lock on the current row (or index key) and not on the table, so records could easily be added or deleted. So if I'm correct the SQL Server would just read the data page by page and never precalculate the total amount and lock it. Hence the "imagined resultset" could be radically (add/delete) changed during the processing (which could take time), and a prior Count would be useless. Just my 2c :)

                    D Offline
                    D Offline
                    Daniel Turini
                    wrote on last edited by
                    #12

                    I deduced the inserts would not be visible and deletes would by this: what would happen with a forward only cursor if I do a SELECT ... ORDER BY table_field and someone inserts data before my current cursor position? Q261186 - Computer Randomly Plays Classical Music

                    M 1 Reply Last reply
                    0
                    • D Daniel Turini

                      I deduced the inserts would not be visible and deletes would by this: what would happen with a forward only cursor if I do a SELECT ... ORDER BY table_field and someone inserts data before my current cursor position? Q261186 - Computer Randomly Plays Classical Music

                      M Offline
                      M Offline
                      Morten Abrahamsen
                      wrote on last edited by
                      #13

                      I agree. If you do an order by and that order by is not the clustered index SQL Server would have to preprocess the entire dataset. (meaning the keys .. not necessarily the data). However, it would be interesting to see what happens if you don't use a postprocessing instruction. (ORDER BY, GROUP BY, UNION, DISTINCT etc) :) Morty

                      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