电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 在线教程-> .NET
ASP.net 2.0 自定义控件的开发之数据分页 第四章-.NET教程,Asp.Net开发
作者:网友供稿 点击:6
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[1] 

 

存储过程 为 sql server 2000版本,请打开sql server 2000 的查询分析器执行下面的sql 语句。
程序用到的存储过程(仅支持主键排序)

if exists (select * from dbo.sysobjects where id = object_id(n[dbo].[sys_quicksortpaging]and objectproperty(id, nisprocedure= 1)
drop procedure [dbo].[sys_quicksortpaging]
go

set quoted_identifier off 
go
set ansi_nulls on 
go

create procedure  sys_quicksortpaging

    
@table nvarchar(4000),            --表名(必须)
    @primarykeyfield nvarchar(50),        --表的主键字段
    @field nvarchar (4000)=*,         --需要返回字段名(必须)        
    @where nvarchar(1000)=null,        --where 条件(可选)
             @groupby nvarchar(1000= null,     --分组
    @orderby nvarchar(1000)=null,     --排序用到的字段()
    @pagenumber int = 1,             --要返回的页(第x页) (默认为第一页)
    @pagesize int = 10,            --每页大小(默认为5)
    @recordcount int output        --返回记录总数
)
as 
    
set nocount on
    
declare @sorttable nvarchar(100)
    
declare @sortname nvarchar(100)
    
declare @strsortcolumn nvarchar(200)
    
declare @operator nvarchar(50)
    
declare @type varchar(100)
    
declare @prec int

    
if @orderby is null or @orderby = 
    
set @orderby =  @primarykeyfield    
    
    
/* 获取用于定位的字段*/                
    
if charindex(desc,@orderby)>0                        
        
begin
            
set @strsortcolumn = replace(@orderbydesc)
            
set @operator = <=
        
end
    
else
        
begin
            
if charindex(asc@orderby= 0
            
set @strsortcolumn = replace(@orderbyasc)
            
set @operator = >=
        
end
    
    
if charindex(.@strsortcolumn> 0                        
        
begin
            
set @sorttable = substring(@strsortcolumn0charindex(.,@strsortcolumn))
            
set @sortname = substring(@strsortcolumncharindex(.,@strsortcolumn+ 1len(@strsortcolumn))
        
end
    
else
        
begin
            
set @sorttable = @table
            
set @sortname = @strsortcolumn
        
end

    
select @type=t.name, @prec=c.prec
    
from sysobjects o 
    
join syscolumns c on o.id=c.id
    
join systypes t on c.xusertype=t.xusertype
    
where o.name = @sorttable and c.name = @sortname
    
    
if charindex(char@type> 0
    
set @type = @type + ( + cast(@prec as nvarchar+ )

    
declare @strstartrow nvarchar(50)
    
declare @strpagesize nvarchar(50)
    
declare @strwhere nvarchar(1000)
    
declare @strwhereand nvarchar(1000)
    
declare @strgroupby nvarchar(1000)

    
if  @pagenumber < 1
    
set @pagenumber = 1
    
    
set @strpagesize = convert (nvarchar(50), @pagesize)
    
set @strstartrow = convert ( nvarchar(50), (@pagenumber - 1)*@pagesize + 1)
    
    
if @where is not null and @where !=
    
begin
        
set @strwhere =  where + @where 
        
set @strwhereand=  and  + @where 
    
end
    
else
    
begin
        
set @strwhere =  
        
set @strwhereand=
    
end
    
    
if @groupby is not null and @groupby != 
    
begin
        
set @strgroupby =  group by  + @groupby
    
end
    
else
    
begin
        
set @strgroupby = 
    
end

    
declare @strsql nvarchar(4000)
    
set @strsql=  select  @recordcount = count (*)  from  + @table + @strwhere +    + @strgroupby
    
exec   sp_executesql   @strsql,n@recordcount   int   output,@recordcount   output--计算总页数   

    
exec
        ( 
        

        declare @sort 
 + @type + 
        set rowcount 
 + @strstartrow + 
        select  @sort = 
 + @strsortcolumn +   from  + @table + @strwhere +    + @strgroupby +  order by  + @orderby +  
        set rowcount 
 + @strpagesize +  
        select 
+@field+  from  + @table +  where  + @strsortcolumn  + @operator  +  @sort  + @strwhereand +    + @strgroupby +  order by  +  @orderby
        )
go
set quoted_identifier off 
go
set ansi_nulls on 
go

 支持任意字段排序的存储过程

if exists (select * from dbo.sysobjects where id = object_id(n[dbo].[sys_sortdatapager]and objectproperty(id, nisprocedure= 1)
drop procedure [dbo].[sys_sortdatapager]
go

set quoted_identifier on 
go
set ansi_nulls on 
go

create procedure sys_sortdatapager (
    
@table nvarchar(4000),            --表名(必须)
    @primarykeyfield nvarchar(50),        --表的主键字段
    @field nvarchar (4000)=*,         --需要返回字段名(必须)
    @where nvarchar(1000)=null,        --where 条件(可选)
    @groupby nvarchar(1000= null,     --分组
    @orderby nvarchar(1000)=null,     --排序用到的字段()
    @pagenumber int = 1,             --要返回的页(第x页) (默认为第一页)
    @pagesize int = 10,            --每页大小(默认为5)
    @recordcount int out            --返回记录总数
)
as

    
/*find the @primarykeyfield type*/
    
declare @pktable varchar(1000)
    
declare @pkname varchar(1000)
    
declare @type varchar(1000)
    
declare @prec int
    
    
if charindex(.@primarykeyfield> 0
        
begin
            
set @pktable = substring(@primarykeyfield0charindex(.,@primarykeyfield))
            
set @pkname = substring(@primarykeyfieldcharindex(.,@primarykeyfield+ 1len(@primarykeyfield))
        
end
    
else
        
begin
            
set @pktable = @table
            
set @pkname = @primarykeyfield
        
end
    
    
select @type=t.name, @prec=c.prec from sysobjects o join syscolumns c on o.id=c.id    join systypes t on c.xusertype=t.xusertype
    
where o.name = @pktable and c.name = @pkname
    
    
if charindex(char@type> 0
    
set @type = @type + ( + cast(@prec as varchar+ )
    
    
declare @strpagesize varchar(50)
    
declare @strstartrow varchar(50)
    
declare @strwhere varchar(1000)
    
declare @strgroupby varchar(1000)
    
    
/*default sorting*/
    
if @orderby is null or @orderby = 
    
set @orderby = @primarykeyfield
    
    
/*default page number*/
    
if @pagenumber < 1
    
set @pagenumber = 1
    
    
/*set paging variables.*/
    
set @strpagesize = cast(@pagesize as varchar(50))
    
set @strstartrow = cast(((@pagenumber - 1)*@pagesize + 1as varchar(50))
    
    
/*set filter & group variables.*/
    
if @where is not null and @where != 
        
set @strwhere =  where  + @where +  
    
else
        
set @strwhere = 
    
if @groupby is not null and @groupby != 
        
set @strgroupby =  group by  + @groupby +  
    
else
        
set @strgroupby = 
        
    
/*execute dynamic query*/    
        
declare @strsql nvarchar(4000)
    
set @strsql=  select  @recordcount = count (*)  from  + @table + @strwhere +    + @strgroupby
    
exec   sp_executesql   @strsql,n@recordcount   int   output,@recordcount   output--计算总页数

    
exec(
    
declare @pagesize int
    set @pagesize = 
 + @strpagesize + 
    
    declare @primarykeyfield 
 + @type + 
    declare @tblpk table (
                pk  
 + @type +  not null primary key
                )
    
    declare pagingcursor cursor dynamic read_only for
    select 
  + @primarykeyfield +  from  + @table + @strwhere +   + @strgroupby +  order by  + @orderby + 
    
    open pagingcursor
    fetch relative 
 + @strstartrow +  from pagingcursor into @primarykeyfield
    
    set nocount on
    
    while @pagesize > 0 and @@fetch_status = 0
    begin
                insert @tblpk (pk)  values (@primarykeyfield)
                fetch next from pagingcursor into @primarykeyfield
                set @pagesize = @pagesize - 1
    end
    
    close       pagingcursor
    deallocate  pagingcursor
    
    select 
 + @field +  from  + @table +  join @tblpk tblpk on  + @primarykeyfield +  = tblpk.pk  + @strwhere +   + @strgroupby +  order by  + @orderby
    )
go
set quoted_identifier off 
go
set ansi_nulls on 
go

创建一个用于测试的表

create table [dbo].[employees] (
    
[employeesid] [numeric](180