利用AspNetPage分页控件,结合高效SQL存储过程

Sql Server2000 存储过程 

-- 用户管理分页 
-- 参数@NewsTypeID 新闻类型
-- 参数@NewsStatus 新闻状态
-- 参数@KeyWord 关键字
-- 参数@Promulgator 发布者
-- 参数@InTime 发布时间
--  @docount = 1  返回记录统计
Alter procedure PR_GetNews
(
    @KeyWord varchar(
100 ),
    @NewsTypeID varchar(
8 ),
    @NewsStatus 
int ,
    @Promulgator varchar(
50 ),
    @InTime datetime,
    @pagesize 
int ,
    @pageindex 
int ,
    @docount bit
)
as
set  nocount on

declare @where nvarchar(
400 )
set  @where = ' where 1=1 '
    
if  @KeyWord  <>   ''   set  @where  =  @where + '  and (Content like  '' % ' + @KeyWord + ' % ''  or Title like  '' % ' + @KeyWord + ' % '' ) '
    
if  @NewsTypeID  <> ''   set  @where = @where + '  and NewsTypeID like  '' % ' + @NewsTypeID + ' % '''      -- 使用模糊查询因为一条新闻可能是多个类型
    
if  @NewsStatus  >=   0   set  @where = @where + '  and NewsStatus=  ' + str(@NewsStatus, 1 +   ''
    
if  @Promulgator  <>   ''   set  @where  =  @where + '  and Promulgator like  '' % '   +  @Promulgator  +   ' % '''
    
if  @InTime  is  not  null   set  @where  =  @where  +   '  and (NewsCheckInTime between  ''' + convert(varchar( 10 ),@InTime, 120 ) + '''  and  ''' + convert(varchar( 10 ),GetDate() + 1 , 120 ) + ''' ) '

declare @RecordCount 
int
declare @TempSql nvarchar(
1000 )

print @where
set  @TempSql = ' select @RecordCount=count(NewsID) from TB_News  ' +  @where
exec sp_executesql @TempSql,N
' @RecordCount int output ' ,@RecordCount output

if (@docount = 1 )
select @RecordCount
else
begin
if (@pageindex = 1 )
exec(
' select top  ' + @pagesize + '  * from TB_News  ' + @where + '  order by NewsID desc ' )
else
begin
declare @PageUpperBound 
int
declare @endrecords 
int
set  @PageUpperBound = @pageindex * @pagesize
if (@PageUpperBound - @pagesize) >= @RecordCount
select 
''
else   if (@RecordCount - (@PageUpperBound - @pagesize) <= @pagesize)
begin
set  @endrecords = @RecordCount - (@PageUpperBound - @pagesize)

exec(
' select  *  from (select top  ' + @endrecords + '  * from TB_News  ' + @where + '  order by NewsID )A order by NewsID desc ' )
end
else
exec(
' select * from (select top  ' + @pagesize + ' * from (select top  ' + @PageUpperBound + '  * from TB_News  ' + @where + '  order by NewsID desc)A order by NewsID )B order by NewsID desc ' )
end
end
set  nocount off
-- select @where

GO
数据访问层
管理分页代码
逻辑层就一个返回就不写了。下面 WEB 层
// 绑定 DataGrid 及 AspNetPage控件
private   void  GridDataBind( string  KeyWord, string  NewsTypeID, int  NewsStatus, string  Promulgator, string  InTime)
        
{                    
            DataSet ds
=null;
            
//获取总记录总数
            MyPage.RecordCount = Pad.BS.Facade.System.FNews.PR_GetNews_ByPageCount(KeyWord,NewsTypeID,NewsStatus,Promulgator,InTime);
            
            ds 
= Pad.BS.Facade.System.FNews.PR_GetNews_ByPage(KeyWord,NewsTypeID,NewsStatus,Promulgator,InTime,MyPage.PageSize,MyPage.CurrentPageIndex);
            
this.gridNews.DataSource = ds;
            
this.gridNews.DataBind();
            MyPage.CustomInfoText
="记录总数:<font color="red"><b>"+MyPage.RecordCount.ToString()+"</b></font>";
            MyPage.CustomInfoText
+=" 总页数:<font color="red"><b>"+MyPage.PageCount.ToString()+"</b></font>";
            MyPage.CustomInfoText
+=" 当前页:<font color="red"><b>"+MyPage.CurrentPageIndex.ToString()+"</b></font>";                            
            
        }


// AspNetPage的 PageChanged 事件
private   void  MyPage_PageChanged( object  src, Pad.WControl.PageChangedEventArgs e)
        
{
            MyPage.CurrentPageIndex 
= e.NewPageIndex;
            
                GridDataBind(
this.txtKeyWord.Text.ToString(),this.listNewsType.SelectedValue.ToString(),
                    Convert.ToInt32(
this.ListStatus.SelectedValue.ToString()),this.txtAuthor.Text.ToString(),
                    
this.txtDate.Text.ToString());
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值