asp.net combining aspnetpager using SQL2005 stored procedure pagination _ Practical Tips

Source: Internet
Author: User
Stored Procedures for SQL2005:
Copy Code code as follows:

Set ANSI_NULLS on
SET QUOTED_IDENTIFIER ON
Go

ALTER PROCEDURE [dbo]. [P_getpagedrecord]
(@startIndex INT,--Start index number
@endindex INT,--End index number
@tblName varchar (255),--table name
@fldName varchar (255),--Display field name
@OrderfldName varchar (255),--Sort field name
@IsReCount bit = 0--Returns the total number of records, not 0 values.
@OrderType bit = 0,--set sort type, not 0 value descending
@strWhere varchar (1000) = '--Query criteria (note: Do not add where)
)
As
DECLARE @strSQL varchar (6000)--subject sentence
DECLARE @strTmp varchar (100)--Temporary variable
DECLARE @strOrder varchar (400)--Sort type
If @OrderType!=0
Begin
Set @strOrder = ' Desc '
End
Else
Begin
Set @strOrder = ' ASC '
End
Set @strSQL = ' with orderlist as (' +
' SELECT row_number (order by ' + @OrderfldName + ' + @strOrder + ') as ROW, ' + @fldName + ' +
' From ' + @tblName
If @strWhere!= '
Set @strSQL = @strSQL + ' where ' + @strWhere
Set @strSQL = @strSQL + ') ' +
' SELECT ' + @fldName + ' +
' From OrderList ' +
' WHERE Row between ' +str (@startIndex) + ' and ' +str (@endIndex) + '


If @IsReCount!= 0
Begin
Set @strSQL = ' Select count (1) as total from [' + @tblName + '] '
If @strWhere!= '
Set @strSQL = @strSQL + ' where ' + @strWhere
End
--print (@strSQL)
EXEC (@strSQL)

key code in use:
Copy Code code as follows:

Page
protected void Anpager_pagechanged (object sender, EventArgs e)
{
Bind ();
}
String strwhere = "1 = 1";
ETHaiNan.BLL.ET_Video BLL = new ETHaiNan.BLL.ET_Video ();
this.anpager.RecordCount = Int. Parse (BLL. GetRecordCount (strwhere). Tables[0]. Rows[0][0]. ToString ());
This.anpager.PageSize = 10;
This.anpager.AlwaysShow = true;
DataSet ds = BLL. GetList (Anpager. Startrecordindex, Anpager. Endrecordindex,strwhere, 0);
This.rpt.DataSource = ds;
Ds. Dispose ();

Data Access Layer:
Copy Code code as follows:

<summary>
Paging Get Data List
</summary>
Public DataSet getlist (int startIndex, int endindex, string strwhere, int isrecount)
{
sqlparameter[] Parameters = {
New SqlParameter ("@startIndex", SqlDbType.Int),
New SqlParameter ("@endindex", SqlDbType.Int),
New SqlParameter ("@tblName", SqlDbType.VarChar, 255),
New SqlParameter ("@fldName", SqlDbType.VarChar, 255),
New SqlParameter ("@OrderfldName", SqlDbType.VarChar, 255),
New SqlParameter ("@IsReCount", Sqldbtype.bit),
New SqlParameter ("@OrderType", Sqldbtype.bit),
New SqlParameter ("@strWhere", sqldbtype.varchar,1000)
};

Parameters[0]. Value = StartIndex;
PARAMETERS[1]. Value = Endindex;
PARAMETERS[2]. Value = "Et_video";
PARAMETERS[3]. Value = "Videoid,video_name,video_typeid,video_ordercode,video_type,video_adddate";
PARAMETERS[4]. Value = "VideoID";
PARAMETERS[5]. Value = Isrecount;
PARAMETERS[6]. Value = 1;
PARAMETERS[7]. Value = strwhere;
Return Dbhelpersql.runprocedure ("P_getpagedrecord", Parameters, "DS");
}

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.