Search code examples
c#sql-serverresultsetsections

Getting Results from Sql Section by Section


I have a question. Is there a way of getting result set from sql (like: has 15 rows in datatable) section by section(such as: i want to show results 5's so if i have 15 rows in my datatable i must have 3 section). Why do i want to do with this way? Because i have a groupbox and it has 5 richtextbox.when i divide the result set with for loop i must have three sections. i will put a next button in group box when i hit them the richtextboxes will be cleared and the second part of the result set(6,7,8,9,10) will be appear in richtextboxes seperately. im waiting your solutions in c#


Solution

  • You may try using offset keyword in T-SQL if you are using SQL Server 2012.

    e.g.

    SELECT First Name + ' ' + Last Name FROM Employees ORDER BY First Name OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY;
    

    You can pass number of rows and offset rows as parameter as well.

    For SQL Server 2008 you may try using row number:

    SELECT some_field
    FROM   (
              SELECT some_field, ROW_NUMBER() OVER (ORDER BY some_id) AS rownum
              FROM   table
           ) AS t
    WHERE  t.rownum BETWEEN 11 AND 20