利用存储过程进行高效分页的例子

高效分页的例子:
后台代码:

None.gif          private   void  Page_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            if(!IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"Page"= "1";            
InBlock.gif                
//绑定跳转到第几页下拉列表    
InBlock.gif
                string SearchContent = " WHERE (Type <> '转帐') AND (Type <> '年费') ";
InBlock.gif                DataSet ds 
= Common.SqlCard.GetNewsRecord(SearchContent,ViewState["Page"].ToString(),"");
InBlock.gif                
int SumNo = int.Parse(ds.Tables[1].Rows[0][0].ToString());
InBlock.gif                ddlNum.Items.Insert(
0"--转到--");
InBlock.gif                
for (int j =0 ;j< ((SumNo-1)/20 + 1) ;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    ddlNum.Items.Insert(j 
+ 1,""+(j + 1).ToString()+"");
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                BindGrid();
ExpandedSubBlockEnd.gif            }

InBlock.gif        
InBlock.gif
ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   void  BindGrid()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string SearchContent = " WHERE (Type <> '转帐') AND (Type <> '年费') ";
InBlock.gif
InBlock.gif
InBlock.gif            
//绑定列表
InBlock.gif
            DataSet ds = Common.SqlCard.GetNewsRecord(SearchContent,ViewState["Page"].ToString(),"");
InBlock.gif
InBlock.gif            DataTable dtList 
= ds.Tables[0];
InBlock.gif            dtList.Columns.Add(
"CustomerName");
InBlock.gif            
for (int i = dtgList.CurrentPageIndex * 20 ; i < dtList.Rows.Count && i < dtgList.CurrentPageIndex * 20 + 20 ; i ++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
InBlock.gif                
//dt.Rows[i]["ShortDate"] = ((DateTime)dt.Rows[i]["Date"]).ToString("yyyy/MM/dd hh:mm tt");
InBlock.gif                
//通过CustomerID值绑定定点或目标账号
InBlock.gif
                if(dtList.Rows[i]["CustomerID"].ToString() != "" )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
//获取商家名称[打折消费、不打折消费]
InBlock.gif
                        dtList.Rows[i]["CustomerName"= Common.SqlCard.GetCustomerName(dtList.Rows[i]["CustomerID"].ToString());
InBlock.gif
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
InBlock.gif                        dtList.Rows[i][
"CustomerName"]="未知数据……";
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif                dtgList.DataSource 
= dtList.DefaultView;
InBlock.gif                dtgList.DataBind();
InBlock.gif
InBlock.gif
InBlock.gif                
//移动的效果
InBlock.gif
                for(int i = 0 ; i < dtgList.Items.Count ; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    dtgList.Items[i].Attributes.Add(
"onMouseOver","this.style.backgroundColor='#F5F5F5'");
InBlock.gif                    dtgList.Items[i].Attributes.Add(
"onMouseOut","this.style.backgroundColor='#FFFFFF'");
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
if(ViewState["Page"].ToString() != "1")
InBlock.gif                    lbnFormerPage.Visible 
= trueelse lbnFormerPage.Visible = false;
InBlock.gif
InBlock.gif                
//绑定页数
InBlock.gif
                int SumNo = int.Parse(ds.Tables[1].Rows[0][0].ToString());
InBlock.gif                lblSumNo.Text 
= SumNo.ToString();
InBlock.gif                lblSumPage.Text 
= ((SumNo-1)/20 + 1).ToString();
InBlock.gif                
if(ViewState["Page"].ToString() != lblSumPage.Text)
InBlock.gif                    lbnNextPage.Visible 
= trueelse lbnNextPage.Visible = false;
InBlock.gif                lblCurrentPage.Text 
= ViewState["Page"].ToString();
InBlock.gif
ExpandedBlockEnd.gif            }

None.gif
ExpandedBlockStart.gifContractedBlock.gif        
Web 窗体设计器生成的代码 #region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.lbnFormerPage.Click += new System.EventHandler(this.lbnFormerPage_Click);
InBlock.gif            
this.lbnNextPage.Click += new System.EventHandler(this.lbnNextPage_Click);
InBlock.gif            
this.ddlNum.SelectedIndexChanged += new System.EventHandler(this.ddlNum_SelectedIndexChanged);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion

None.gif
None.gif
None.gif        
private   void  lbnFormerPage_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            ViewState[
"Page"= int.Parse(ViewState["Page"].ToString()) - 1;
InBlock.gif            BindGrid();
ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   void  lbnNextPage_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            ViewState[
"Page"= int.Parse(ViewState["Page"].ToString()) + 1;
InBlock.gif            BindGrid();
ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   void  ddlNum_SelectedIndexChanged( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
//Response.Write("<script>alert('" + ddlNum.SelectedIndex + "')</script>");
InBlock.gif
            ViewState["Page"= ddlNum.SelectedIndex;
InBlock.gif            BindGrid();
ExpandedBlockEnd.gif        }

None.gif
None.gif    
数据操作层的代码:
ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// 获得最新消费记录
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="SearchContent"></param>
InBlock.gif        
/// <param name="Page"></param>
InBlock.gif        
/// <param name="Orderby"></param>
ExpandedBlockEnd.gif        
/// <returns></returns>

None.gif          public   static  DataSet GetNewsRecord( string  SearchContent, string  Page, string  Orderby)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            SqlParameter[] mySqlParameter 
= new SqlParameter[7];
InBlock.gif            mySqlParameter[
0= new SqlParameter("@PageSize",20);
InBlock.gif            mySqlParameter[
1= new SqlParameter("@PageIndex",Page);
InBlock.gif            
//查询条件
InBlock.gif
            mySqlParameter[2= new SqlParameter("@Condition",SearchContent);
InBlock.gif
InBlock.gif            
//表名
InBlock.gif
            mySqlParameter[3= new SqlParameter("@TheTable","card_Record ");
InBlock.gif
InBlock.gif            
//选择的字段
InBlock.gif
            mySqlParameter[4= new SqlParameter("@SelectField","CardNo,Date,CustomerID,Consumption,Discount,Type,ScoreGet,ScoreLeft");
InBlock.gif
InBlock.gif            
//排序方式
InBlock.gif
            if(Orderby == "")
InBlock.gif                Orderby 
= "order by [Date] desc";
InBlock.gif            mySqlParameter[
5= new SqlParameter("@OrderBy",Orderby);
InBlock.gif            mySqlParameter[
6= new SqlParameter("@TableID","ID");
InBlock.gif
InBlock.gif            
return ExecuteStoreDataset("card_高效分页",mySqlParameter);
ExpandedBlockEnd.gif        }

ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// 执行存储过程,返回DataSet数据集
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="StoreName"></param>
InBlock.gif        
/// <param name="par"></param>
ExpandedBlockEnd.gif        
/// <returns></returns>

None.gif          protected   static  DataSet ExecuteStoreDataset( string  StoreName,SqlParameter[] par)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            SqlConnection conn 
= CreateConnection();
InBlock.gif            
InBlock.gif            SqlDataAdapter myDataAdapter 
= new SqlDataAdapter(StoreName,conn);
InBlock.gif            myDataAdapter.SelectCommand.CommandType 
= CommandType.StoredProcedure;
InBlock.gif            
for(int i=0;i<par.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                myDataAdapter.SelectCommand.Parameters.Add(par[i]);
ExpandedSubBlockEnd.gif            }

InBlock.gif            DataSet ds 
= new DataSet();
InBlock.gif            myDataAdapter.Fill(ds);
InBlock.gif            
return ds;
ExpandedBlockEnd.gif        }
存储过程:
None.gif CREATE   PROCEDURE   [ dbo ] . [ card_高效分页 ]  
None.gif(
None.gif    
@PageSize   int ,                                      -- 每页记录
None.gif
     @PageIndex   int ,                                    -- 当前页数,1开始
None.gif
     @Condition   varchar ( 8000 ),                    -- 查询条件,包括and,where,必须有一个条件如where 2>1
None.gif
     @TheTable   varchar ( 8000 ),                     -- 表名
None.gif
     @SelectField   varchar ( 8000 ),                 -- 要选择的字段
None.gif
     @OrderBy   varchar  ( 8000 ),                     -- OrderBy字句,包括order
None.gif
     @TableID   varchar  ( 8000 )                         -- table主键
None.gif
)
None.gif
AS
None.gif
begin
None.gif
declare   @Sql   varchar ( 8000 )
None.gif
None.gif
-- 返回记录
None.gif
set   @Sql = ' select top  '   +   cast ( @PageSize   as   varchar ( 10 ))  +   '   '   +   @SelectField   +   '  from  '   +   @TheTable   +   '   '   +   @Condition   +   '  and  '
None.gif    
+   @TableID   + '  not in (select top  '   +   cast (( @PageSize * ( @PageIndex - 1 ))  as   varchar ( 10 )) + '   '   + @TableID + '   from  '   +   @TheTable   +   '   '   +   @Condition
None.gif    
+   '   '   +   @OrderBy   + ' '   +   @OrderBy
None.gif
exec ( @sql )
None.gif
None.gif
-- 返回总数
None.gif
set   @Sql = ' select count( '   +   @TableID   +   ' ) from  '   +   @TheTable   + '   '   +   @Condition
None.gif
exec ( @sql )
None.gif
end
None.gif
GO
None.gif

前台代码(部分):
None.gif                                              < td  align ="right" >
None.gif                                                
< TABLE  height ="23"  cellSpacing ="0"  cellPadding ="0"  border ="0" >
None.gif                                                    
< tr >
None.gif                                                        
< td  width ="3" >< IMG  src ="../images/SelectBg.gif" ></ td >
None.gif                                                        
< td  vAlign ="middle"  background ="../images/SelectBorder.gif"  bgColor ="white" >< span  style ="LEFT: 3px; OVERFLOW: hidden; POSITION: relative; HEIGHT: 17px" >< asp:dropdownlist  id ="ddlNum"  Runat ="server"  AutoPostBack ="True"  CssClass ="Border" ></ asp:dropdownlist ></ span ></ td >
None.gif                                                        
< td  width ="3" >< IMG  style ="FILTER: fliph"  src ="../images/SelectBg.gif" ></ td >
None.gif                                                    
</ tr >
None.gif                                                
</ TABLE >
None.gif                                            
</ td >
None.gif

转载于:https://www.cnblogs.com/zhongru_tu/archive/2005/04/26/145403.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值