In Asp.net, I am trying to achieve pagination using following code:
String query="SELECT * "+
"FROM (SELECT Distinct emp_name, emp_address, "+
" ROW_NUMBER() OVER(ORDER BY emp_id) AS rownum"+
" FROM Employee"+
" )as Person "+
"WHERE rownum>"+ start +" and rownum <="+ end +";
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
Above code does not retrieve Distinct rows.
How can I adjust my query to get distinct emp_name and order by emp_id along with total number of entries in single ExecuteReader()?
Currently I am calling ExecuteReader() twice first for data and and second for total count.
I followed SQL Server DISTINCT pagination with ROW_NUMBER() not distinct but was unable to understand how to implement it in my code. Please help.
Use
String.Formatand raw string (@) to avoid concatenation.Use
NextResultto read multiple result sets.