DataGrid custom paging stored procedure

Source: Internet
Author: User
 
Stored Procedure code:
CREATE proc page
@ RecordCount int output,
@ QueryStr nvarchar (100) = 'table1', -- table name, View name, and query statement
@ PageSize int = 20, -- size of each page (number of rows)
@ PageCurrent int = 1, -- the page to be displayed
@ FdShow nvarchar (1000) = '*', -- List of fields to be displayed
@ IdentityStr nvarchar (100) = 'id', -- primary key
@ WhereStr nvarchar (200) = '1 = 1 and id % 11111 = 0 ',
@ FdOrder nvarchar (100) = 'id', -- sort
@ IsReturn bit = 0
As
Declare
@ SQL nvarchar (2000)
Set @ SQL =''
If @ WhereStr = ''begin
Set @ WhereStr = '1 = 1'
End
Declare @ tsql nvarchar (200)
If (@ isReturn = 1) begin
Set @ tsql = n' select @ RecordCount = count (*) from '+ @ QueryStr + 'where' + @ WhereStr
Exec sp_executesql @ tsql, n' @ RecordCount int output', @ RecordCount output
End
Else begin
Set @ RecordCount = @ PageSize * @ PageCurrent + 1
End
If @ PageCurrent = 1 begin
Set @ SQL = 'SELECT top '+ cast (@ PageSize as nvarchar (3 )) + ''+ @ FdShow + 'from' + @ QueryStr + 'where' + @ WhereStr + 'Order by' + @ IdentityStr
End
Else begin
Set @ SQL = 'SELECT top '+ cast (@ PageSize as nvarchar (3 )) + ''+ @ FdShow + 'from' + @ QueryStr + 'where' + @ WhereStr + 'and' + @ IdentityStr + '> (select max (' + @ IdentityStr +') from (select top '+ cast (@ PageSize * (@ PageCurrent-1) as nvarchar (10 )) + ''+ @ IdentityStr + 'from' + @ QueryStr + 'where' + @ WhereStr + 'Order by' + @ IdentityStr + ') as t) order by' + @ IdentityStr
End
If @ FdOrder <> ''and @ FdOrder <> @ IdentityStr begin
Set @ SQL = 'select * from ('+ @ SQL +') as t4867435348493 order by '+ @ FdOrder
End
-- Print @ SQL
Execute (@ SQL)
GO
Background code:
Protected System. Web. UI. WebControls. DataGrid DataGrid1;
 
Private void Page_Load (object sender, System. EventArgs e)

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.