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 number of affected rows

How to get number of affected rows

Scheduled Pinned Locked Moved Database
databasehelptutorialannouncement
4 Posts 4 Posters 1 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.
  • U Offline
    U Offline
    User 1197946
    wrote on last edited by
    #1

    Can anyone tell me how to get the number of rows affected by select query using datareader.when we insert,update or delete we can get the number of rows affected,but not when we use select query.So can anyone help me urgent problemguy

    R S S 3 Replies Last reply
    0
    • U User 1197946

      Can anyone tell me how to get the number of rows affected by select query using datareader.when we insert,update or delete we can get the number of rows affected,but not when we use select query.So can anyone help me urgent problemguy

      R Offline
      R Offline
      Roland Bar
      wrote on last edited by
      #2

      I've solved this problem once in an ugly way: I wanted to give back the total number of rows but return only the first 100. So I created a stored Procedure that made first the query with select count(*) and then the same query but with the needed columns and as the last column the result of the first query. ex: declare @num as int select @num = count(*) from Users where groupID = 10 select top 100 username, address , @num from Users where groupID = 10 Hope this helps a little bit Greets Roland

      1 Reply Last reply
      0
      • U User 1197946

        Can anyone tell me how to get the number of rows affected by select query using datareader.when we insert,update or delete we can get the number of rows affected,but not when we use select query.So can anyone help me urgent problemguy

        S Offline
        S Offline
        Steven Campbell
        wrote on last edited by
        #3

        The datareader does not know the number of rows until it gets them all. If you really need the (expected) number of rows, run a SELECT COUNT(*) query.


        my blog

        1 Reply Last reply
        0
        • U User 1197946

          Can anyone tell me how to get the number of rows affected by select query using datareader.when we insert,update or delete we can get the number of rows affected,but not when we use select query.So can anyone help me urgent problemguy

          S Offline
          S Offline
          sivilian
          wrote on last edited by
          #4

          One way to do it is to put in a return field which counts the records that are returned. For instance add Count(FIeldName) As RowsAffected as one of your fields. This will however return an extra field for each row, which may not be a good idea. The reason there is no way for the datareader to know how may rows are returned is that it is still connected to the data and not all rows are retrieved yet. You could count the rows as you use them, through a loop, or if you bind them to a control, count them that way. hope this helps, sivilian

          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