MSSQL Access Top page pagination method

Source: Internet
Author: User
Tags mssql static class

MSSQL Access Top page pagination method
The double top method can be passed into an SQL statement to make a SQL statement, or a multiple-field order, compared to the not in and Max methods.
But pros and cons, it requires that the sort field must form a unique record, and that fields that have the same name as the sorted field are not allowed in the list of fields after the select.
Although Sql2k5 and above have provided rownum () for paging, the use of SQL2K has been developed more

using System;
Using System.Collections.Generic;
Using System.Text;

<summary>
To construct a page-after SQL statement
</summary>
public static Class Paginghelper
{
<summary>
To get the paging SQL statement, the sort field needs to form a unique record
</summary>
<param name= "_recordcount" > Total Records </param>
<param name= "_pagesize" > Number of records per page </param>
<param name= "_pageindex" > Current page </param>
<param name= "_safesql" >sql query statement </param>
<param name= "_orderfield" > Sort fields, multiple separated by "," </param>
<returns> Paging SQL statements </returns>
public static string Createpagingsql (int _recordcount, int _pagesize, int _pageindex, string _safesql, String _orderfield)
{
Rearrange sorted fields to prevent errors
string[] Arrstrorders = _orderfield.split (new char[] {', '}, stringsplitoptions.removeemptyentries);
StringBuilder sboriginalorder = new StringBuilder (); Original Sort Field
StringBuilder Sbreverseo Tutorial Rder = new StringBuilder (); In contrast to the original sort field, for paging
for (int i = 0; i < arrstrorders.length; i++)
{
Arrstrorders[i] = Arrstrorders[i].trim (); Remove space before and after
if (i!= 0)
{
Sboriginalorder.append (",");
Sbreverseorder.append (",");
}
Sboriginalorder.append (Arrstrorders[i]);

int index = Arrstrorders[i].indexof (""); Determine if there is a lifting mark
if (Index > 0)
{
Replace the lifting identification, paging required
BOOL flag = arrstrorders[i].indexof ("desc", StringComparison.OrdinalIgnoreCase)!=-1;
Sbreverseorder.appendformat ("{0} {1}", Arrstrorders[i].remove (index), flag?) "ASC": "desc");
}
Else
{
Sbreverseorder.appendformat ("{0} desc", Arrstrorders[i]);
}
}

Calculate Total Pages
_pagesize = _pagesize = = 0? _recordcount: _pagesize;
int PageCount = (_recordcount + _pagesize-1)/_pagesize;

Check the current page number
if (_pageindex < 1)
{
_pageindex = 1;
}
else if (_pageindex > PageCount)
{
_pageindex = PageCount;
}

StringBuilder sbsql = new StringBuilder ();
First page, use top n without paging query
if (_pageindex = 1)
{
Sbsql.appendformat ("Select top {0} *", _pagesize);
Sbsql.appendformat ("from ({0}) as T", _safesql);
Sbsql.appendformat ("ORDER by {0}", sboriginalorder.tostring ());
}
Last page, reduce a top n
else if (_pageindex = = PageCount)
{
Sbsql.append ("SELECT * from");
Sbsql.append ("(");
Sbsql.appendformat ("Select top {0} *", _recordcount-_pagesize * (_pageindex-1));
Sbsql.appendformat ("from ({0}) as T", _safesql);
Sbsql.appendformat ("ORDER by {0}", sbreverseorder.tostring ());
Sbsql.append (") as T");
Sbsql.appendformat ("ORDER by {0}", sboriginalorder.tostring ());
}
Pagination at first half of page
else if (_pageindex < (PAGECOUNT/2 + pagecount% 2))
{
Sbsql.append ("SELECT * from");
Sbsql.append ("(");
Sbsql.appendformat ("Select top {0} * from", _pagesize);
Sbsql.append ("(");
Sbsql.appendformat ("Select top {0} *", _pagesize * _pageindex);
Sbsql.appendformat ("from ({0}) as T", _safesql);
Sbsql.appendformat ("ORDER by {0}", sboriginalorder.tostring ());
Sbsql.append (") as T");
Sbsql.appendformat ("ORDER by {0}", sbreverseorder.tostring ());
Sbsql.append (") as T");
Sbsql.appendformat ("ORDER by {0}", sboriginalorder.tostring ());
}
Pagination at second half of page
Else
{
Sbsql.appendformat ("Select top {0} * from", _pagesize);
Sbsql.append ("(");
Sbsql.appendformat ("Select top {0} *" ((_recordcount% _pagesize) + _pagesize * (PageCount-_pageindex));
Sbsql.appendformat ("from ({0}) as T", _safesql);
Sbsql.appendformat ("ORDER by {0}", sbreverseorder.tostring ());
Sbsql.append (") as T");
Sbsql.appendformat ("ORDER by {0}", sboriginalorder.tostring ());
}

return sbsql.tostring ();
}

<summary>
Get the total number of records SQL statement
</summary>
<param name= "_n" > Limited number of records </param>
<param name= "_safesql" >sql query statement </param>
<returns> Total Records SQL statements </returns>
public static string Createtopnsql (int _n, string _safesql)
{
Return String.Format ("select top {0} * FROM ({1}) as T", _n, _safesql);
}

   ///<summary>
   ///Get total number of records SQL statement
   ///</ Summary>
   ///<param name= "_safesql" >sql query statements </param>
   /// <returns> total number of records SQL statement </RETURNS>
    public static string Createcountingsql (String _ Safesql)
    {
        return String.Format ("SELECT Count" ( 1 as RecordCount from ({0}) as T ", _safesql);
   }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.