How to get number of affected rows
-
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
-
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
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 -
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
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.
-
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
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