Click here to Skip to main content
15,943,406 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do you get the row count of the resultset returned from a select statement? I want both the resultset and the count.
Mike Meinz 3-Sep-13 14:42pm    
Maybe if you told us why you need the row count before you read the rows, we could offer best practice suggestions based on our experience.
RedDk 3-Sep-13 18:23pm    

SELECT IDENTITY([int],1,1) AS [Idx], [stuff], [morestuff] INTO #tblTemp
FROM [tblOriginalSelectStuff]

Have both count (normally I'd SELECT COUNT(*) FROM etc) and that count's in a column of it's own. As a good index also.

1 solution

Depends on how you are reading the results. If you are using the SqlDataReader, then you have to iterate through each row and count them manually as you process them from the reader - SqlDataReader has no row count, as it doesn't read all the records at once so it doesn't know how manythere are until it's read the last one.

If you use a DataAdapter and a DataSet or DataTable, then your can fill it and use the DataTable.Rows.Count property at any time.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900