利用存储过程进行分页
1,要新建存储过程
如下:
/*-----------------------------------------------------------------------------------*/
--=========通用分页存储过程=========
CREATE procedure GetRecordByPage
@tblName varchar(100), -- 表名
@fldCow varchar(100)='*', -- 要查询的列
@fldName varchar(255), -- 排序列
@GroupStr varchar(255)='', -- 组列
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@OrderType bit = 1, -- 设置排序类型, 1则降序
@strWhere varchar(2000) = '',-- 查询条件 (注意: 不要加 where)
@ReturnCoutn int output
AS
declare @strSQL varchar(3000) -- 主语句
declare @strTmp varchar(1000) -- 临时变量
declare @strOrder varchar(500) -- 排序类型
declare @strGrop varchar(500) -- 排序类型
--declare @ReturnCoutn1 int --返回总条数
declare @SQLJL nvarchar(500)--记录查询条数的SQL
if len(@GroupStr)>0
set @strGrop=' group by '+@GroupStr+' '
else
set @strGrop=''
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName + '] desc'
set @strOrder=@strGrop+@strOrder
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName + '] asc'
set @strOrder=@strGrop+@strOrder
end
set @strSQL = 'select top ' + str(@PageSize) + ' '+@fldCow+' from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
+ @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) + ' '+@fldCow+ ' from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
if @PageIndex = 1
begin
set @strTmp = ''
if @strWhere != ''
set @strTmp = ' where (' + @strWhere + ') '
set @strSQL = 'select top ' + str(@PageSize) + ' '+@fldCow+ ' from ['
+ @tblName + ']' + @strTmp + ' ' + @strOrder
end
set @SQLJL=N'select @ReturnCoutn= count(*) from ' + @tblname+ ' where '+@strWhere+''
exec sp_executesql @SQLJL,N'@ReturnCoutn int output',@ReturnCoutn output
exec (@strSQL )
GO
/*-----------------------------------------------------------------------------------*/
以下是CS代码:
/*-----------------------------------------------------------------------------------*/
namespace TestWebPage
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// WebUserControl1 的摘要说明。
/// </summary>
public class WebUserControl1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Button btnsye;
protected System.Web.UI.WebControls.Button btnNextPag;
protected System.Web.UI.WebControls.TextBox txtPageSex;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Button btnTz;
protected System.Web.UI.WebControls.HyperLink HyperLink1;
string strPage="0";//接受参数
int I=1;//记录For循环的开始数
int index=0;
protected System.Web.UI.WebControls.Label lblZs;
protected System.Web.UI.WebControls.Label lblYS;//记录从某个开始
int count=0;
protected System.Web.UI.WebControls.HyperLink hlinkWy;
int PageS=0;//页数
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
strPage=Request.QueryString["objPageindex"];
if(strPage==null)
{
Session["pageindex"]=1;
DataBind(ReturnDs("t_XmX","*","Xid","",10,1,0," 1=1"));
}
else
{
Session["pageindex"]=strPage;
DataBind(ReturnDs("t_XmX","*","Xid","",10,int.Parse(strPage),0," 1=1"));
}
PageTz();
}
}
public void PageTz()
{
this.Label3.Text=string.Empty;
index=int.Parse(Session["pageindex"].ToString());
if(Session["index"]!=null)
{
if(int.Parse(Session["index"].ToString())+5>int.Parse(strPage))
{
if(int.Parse(strPage)-5>1)
I=int.Parse(strPage)-5;
}
if(index-int.Parse(Session["index"].ToString())>5)
{
I=index-5;
}
}
for(int i=I;i<=PageS;i++)
{
if(Session["pageindex"]!=null)
{
if(i==int.Parse(Session["pageindex"].ToString()))
this.Label3.Text +=i+" ";
else
this.Label3.Text +=@"<a href='http://localhost:7003/TestWebPage/WebForm1.aspx?objPageindex="+i+"'>"+i+"</a>"+" ";
}
else
{
if(i==1)
this.Label3.Text +=i+" ";
else
this.Label3.Text +=@"<a href='http://localhost:7003/TestWebPage/WebForm1.aspx?objPageindex="+i+"'>"+i+"</a>"+" ";
}
if(i-I==8)
break;
}
Session["index"]=I;//记录开始的页码
this.HyperLink1.NavigateUrl="http://localhost:7003/TestWebPage/WebForm1.aspx?objPageindex=1";
this.hlinkWy.NavigateUrl="http://localhost:7003/TestWebPage/WebForm1.aspx?objPageindex="+PageS+"";
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnsye.Click += new System.EventHandler(this.btnsye_Click);
this.btnNextPag.Click += new System.EventHandler(this.btnNextPag_Click);
this.btnTz.Click += new System.EventHandler(this.btnTz_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public SqlConnection Con()
{
SqlConnection con=new SqlConnection("server=.;database=TestWebPage;uid=sa;pwd=sa;");
con.Open();
return con;
}
/// <summary>
///
/// </summary>
/// <param name="tblname">表名</param>
/// <param name="fldcow">要查询的列</param>
/// <param name="flname">要排序的列</param>
/// <param name="groupstr">组列</param>
/// <param name="pagesize">每一页显示的最多记录数</param>
/// <param name="pageindex">页码</param>
/// <param name="ordertype">设置排序类型, 1则降序</param>
/// <param name="strwhere">查询条件 (注意: 不要加 where)</param>
/// <returns></returns>
public DataSet ReturnDs(string tblname,string fldcow,string flname,string groupstr,int pagesize,int pageindex,int ordertype,string strwhere)
{
DataSet ds=new DataSet();
try
{
SqlCommand cmd=new SqlCommand();
cmd.CommandText="GetRecordByPage";
cmd.CommandType=CommandType.StoredProcedure;
cmd.Connection=Con();
cmd.Parameters.Add(new SqlParameter("@tblName",SqlDbType.VarChar));
cmd.Parameters["@tblName"].Value=tblname;
cmd.Parameters.Add(new SqlParameter("@fldCow",SqlDbType.VarChar));
cmd.Parameters["@fldCow"].Value=fldcow;
cmd.Parameters.Add(new SqlParameter("@fldName",SqlDbType.VarChar));
cmd.Parameters["@fldName"].Value=flname;
cmd.Parameters.Add(new SqlParameter("@GroupStr",SqlDbType.VarChar));
cmd.Parameters["@GroupStr"].Value=groupstr;
cmd.Parameters.Add(new SqlParameter("@PageSize",SqlDbType.Int));
cmd.Parameters["@PageSize"].Value=pagesize;
cmd.Parameters.Add(new SqlParameter("@PageIndex",SqlDbType.Int));
cmd.Parameters["@PageIndex"].Value=pageindex;
cmd.Parameters.Add(new SqlParameter("@OrderType",SqlDbType.Bit));
cmd.Parameters["@OrderType"].Value=ordertype;
cmd.Parameters.Add(new SqlParameter("@strWhere",SqlDbType.VarChar));
cmd.Parameters["@strWhere"].Value=strwhere;
// cmd.Parameters.Add(new SqlParameter("@ReturnCoutn",SqlDbType.Int,4,ParameterDirection.ReturnValue,false,0,0,string.Empty,DataRowVersion.Default,null));
cmd.Parameters.Add(new SqlParameter("@ReturnCoutn",SqlDbType.Int));
cmd.Parameters["@ReturnCoutn"].Direction=ParameterDirection.Output;
cmd.Parameters["@ReturnCoutn"].Value=0;
cmd.ExecuteNonQuery();
count=int.Parse(cmd.Parameters["@ReturnCoutn"].Value.ToString());
this.lblZs.Text =count.ToString();//得到总记录
SqlDataAdapter da=new SqlDataAdapter(cmd);
da.Fill(ds);
}
catch(Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex);
}
finally
{
Con().Close();
}
this.lblZs.Text=this.count.ToString();//得到总记录
PageS=count/10;
int yu=count%10;
if(yu>0)
{
PageS=PageS+1;
this.lblYS.Text=Convert.ToString(PageS);//计算总页数
}
else
this.lblYS.Text=PageS.ToString();
return ds;
}
private void btnsye_Click(object sender, System.EventArgs e)
{
if(Session["pageindex"].ToString()!="1")
{
Session["pageindex"]=int.Parse(Session["pageindex"].ToString())-1;
DataBind(ReturnDs("Water_Menu","*","id","",10,int.Parse(Session["pageindex"].ToString()),0," 1=1"));
}
}
public void DataBind(DataSet ds)
{
this.DataGrid1.DataSource=ds.Tables[0].DefaultView;
this.DataGrid1.DataBind();
Label1.Text=Session["pageindex"].ToString();
}
private void btnTz_Click(object sender, System.EventArgs e)
{
Session["pageindex"]=this.txtPageSex.Text.Trim();
DataBind(ReturnDs("t_XmX","*","Xid","",10,int.Parse(this.txtPageSex.Text.Trim()),0," 1=1"));
PageTz();
}
private void btnNextPag_Click(object sender, System.EventArgs e)
{
int i=int.Parse(Session["pageindex"].ToString());
Session["pageindex"]=i+1;
DataBind(ReturnDs("Water_Menu","*","id","",10,int.Parse(Session["pageindex"].ToString()),0," 1=1"));
}
private void btnShouye_Click(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Session["pageindex"]=1;
DataBind(ReturnDs("Water_Menu","*","id","",10,1,0," 1=1"));
}
}
}
}
/*-----------------------------------------------------------------------------------*/
以下是页面代码:
/*-----------------------------------------------------------------------------------*/
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="WebUserControl1.ascx.cs" Inherits="TestWebPage.WebUserControl1" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
当前 |
总数 |
每页10条 | 共 |
/*-----------------------------------------------------------------------------------*/