In the .Net framework, the SQL Data Reader is a lightweight forward-only stream from SQL Server. This means that after reading row 1 of the data set and advancing to row 2 you will not be able to return to row 1. This is a limitation for developers, but the trade-off is that the SQL Data Reader is very efficient and lightweight.
- Skill level:
Things you need
- SQL Server
Query the database and return an SQLDataReader object from the query.
Iterate the SQLDataReader to get the total number of rows. The SQLDataReader is forward-only, so you will also need to consume the data in this loop. Here is a code sample for iterating the SQLDataReader:
Dim i As Integer
While sqlDataReader.Read 'Consume data here if needed i += 1 End While Response.Write("Total Rows: " & i)
Close the SQLDataReader when you are done with it by calling sqlDataReader.Close()
Tips and warnings
- If you only need a count you can execute a query with just "count(1)" in the select clause, then execute the query as Command.ExecuteScalar to return the count.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for