使用C#和存储过程实现分页。简化你的代码。

在最近的项目中,由于要用到自定义分页的功能,本人就在网上找了个存储过程。结合C#写了个分页类。由于本人第一次写文章。写得不好,大家不要扔鸡蛋。。

下面是存储过程(sqlserver2000下通过)

 

None.gif--最通用的分页存储过程
None.gif-- 获取指定页的数据 
None.gif
None.gifCREATE PROCEDURE Pagination 
None.gif
None.gif@tblName   varchar(255),       -- 表名 
None.gif
None.gif@strGetFields varchar(1000) = '*',  -- 需要返回的列 
None.gif
None.gif@fldName varchar(255)='',      -- 排序的字段名 
None.gif
None.gif@PageSize   int = 10,          -- 页尺寸 
None.gif
None.gif@PageIndex  int = 1,           -- 页码 
None.gif
None.gif@doCount  bit = 0,   -- 返回记录总数, 非 0 值则返回 
None.gif
None.gif@OrderType bit = 0,  -- 设置排序类型, 非 0 值则降序 
None.gif
None.gif@strWhere  varchar(1500) = ''  -- 查询条件 (注意: 不要加 where) 
None.gif
None.gifAS 
None.gif
None.gifdeclare @strSQL   varchar(5000)       -- 主语句 
None.gif
None.gifdeclare @strTmp   varchar(110)        -- 临时变量 
None.gif
None.gifdeclare @strOrder varchar(400)        -- 排序类型 
None.gif
None.gif
None.gif
None.gifif @doCount != 0 
None.gif
None.gif begin 
None.gif
None.gif   if @strWhere !='' 
None.gif
None.gif   set @strSQL = 'select count(*) as Total from ['+ @tblName +'] where '+ @strWhere 
None.gif
None.gif   else 
None.gif
None.gif   set @strSQL = 'select count(*) as Total from ['+ @tblName +']'
None.gif
None.gifend 
None.gif
None.gif--以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都
None.gif--是@doCount为0的情况 
None.gif
None.gifelse 
None.gif
None.gifbegin 
None.gif
None.gif
None.gif
None.gifif @OrderType != 0 
None.gif
None.gifbegin 
None.gif
None.gif   set @strTmp = '<(select min' 
None.gif
None.gifset @strOrder = ' order by ['+ @fldName +'] desc'
None.gif
None.gif--如果@OrderType不是0,就执行降序,这句很重要! 
None.gif
None.gifend 
None.gif
None.gifelse 
None.gif
None.gifbegin 
None.gif
None.gif   set @strTmp = '>(select max'
None.gif
None.gif   set @strOrder = ' order by ['+ @fldName +'] asc'
None.gif
None.gifend 
None.gif
None.gif
None.gif
None.gifif @PageIndex = 1 
None.gif
None.gifbegin 
None.gif
None.gif   if @strWhere != ''   
None.gif
None.gif    set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ '  from ['+ @tblName +'] where ' + @strWhere + ' ' + @strOrder 
None.gif
None.gif    else 
None.gif
None.gif    set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ '  from ['+ @tblName +'] '+ @strOrder 
None.gif
None.gif--如果是第一页就执行以上代码,这样会加快执行速度 
None.gif
None.gifend 
None.gif
None.gifelse 
None.gif
None.gifbegin 
None.gif
None.gif--以下代码赋予了@strSQL以真正执行的SQL代码 
None.gif
None.gifset @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ '  from [' + @tblName +'] where [' + @fldName + ']' + @strTmp + '(['+ @fldName + ']) 
None.giffrom (select top ' + str((@PageIndex-1)*@PageSize) + ' ['+ @fldName + '] from ['+ @tblName +']' + @strOrder + ') as tblTmp)'+ @strOrder 
None.gif
None.gif
None.gif
None.gifif @strWhere != '' 
None.gif
None.gif   set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ '  from ['+ @tblName +'] where [' + @fldName + ']' + @strTmp + '(['+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['+ @fldName + '] 
None.giffrom ['+ @tblName +'] where ' + @strWhere + ' ' + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder 
None.gif
None.gifend 
None.gif
None.gifend   
None.gif
None.gifexec ( @strSQL)
None.gifGO
None.gif

 

下面是C#的代码

None.gifusing System.Data ;
None.gifusing System.Data.SqlClient ;
None.gifusing Microsoft.ApplicationBlocks.Data ;
None.gifusing System.Web ;
None.gifusing System.Web.UI ;
None.gifnamespace RssLayer.PageHelper
ExpandedBlockStart.gifContractedBlock.gifSqlserver存储过程和C#分页类简化你的代码!_字段_126{
ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
InBlock.gif    /// 分页类PagerHelper 的摘要说明。
ExpandedSubBlockEnd.gif    /// </summary>
InBlock.gif    public class PagerHelper
ExpandedSubBlockStart.gifContractedSubBlock.gif    Sqlserver存储过程和C#分页类简化你的代码!_字段_126{
InBlock.gif        private string connectionString;
InBlock.gif
InBlock.gif    
InBlock.gif
InBlock.gif        public PagerHelper(string tblname,string sortname,bool docount,string connectionString)
ExpandedSubBlockStart.gifContractedSubBlock.gif        Sqlserver存储过程和C#分页类简化你的代码!_字段_126{
InBlock.gif            this.tblName = tblname;
InBlock.gif            this.fldName = sortname ;
InBlock.gif            this.connectionString  = connectionString ;
InBlock.gif            this.docount = docount;
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        public PagerHelper(string tblname,bool docount,
InBlock.gif            string strGetFields,    string fldName,int pagesize,
InBlock.gif            int pageindex,bool ordertype,string strwhere,string connectionString        
InBlock.gif            )
ExpandedSubBlockStart.gifContractedSubBlock.gif        Sqlserver存储过程和C#分页类简化你的代码!_字段_126{
InBlock.gif            this.tblName = tblname ;            
InBlock.gif            this.docount = docount ;
InBlock.gif            this.strGetFields = strGetFields ;
InBlock.gif            this.fldName = fldName;
InBlock.gif            this.pagesize = pagesize ;
InBlock.gif            this.pageindex = pageindex;
InBlock.gif            this.ordertype = ordertype ;
InBlock.gif            this.strwhere = strwhere ;
InBlock.gif            this.connectionString  = connectionString ;
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 得到记录集的构造函数
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="tblname"></param>
InBlock.gif        /// <param name="strwhere"></param>
ExpandedSubBlockEnd.gif        /// <param name="connectionString"></param>
InBlock.gif        public PagerHelper(string tblname,string strwhere,string connectionString)    
ExpandedSubBlockStart.gifContractedSubBlock.gif        Sqlserver存储过程和C#分页类简化你的代码!_字段_126{
InBlock.gif            this.tblName = tblname;
InBlock.gif            this.strwhere = strwhere ;
InBlock.gif            this.docount = true;
InBlock.gif            this.connectionString  = connectionString ;
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        private string tblName;
InBlock.gif        public string TblName
ExpandedSubBlockStart.gifContractedSubBlock.gif        Sqlserver存储过程和C#分页类简化你的代码!_字段_126{
ExpandedSubBlockStart.gifContractedSubBlock.gif            getSqlserver存储过程和C#分页类简化你的代码!_字段_126{return tblName;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            setSqlserver存储过程和C#分页类简化你的代码!_字段_126{tblName =value;}
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        private string strGetFields="*";
InBlock.gif        public string StrGetFields
ExpandedSubBlockStart.gifContractedSubBlock.gif        Sqlserver存储过程和C#分页类简化你的代码!_字段_126{
ExpandedSubBlockStart.gifContractedSubBlock.gif            getSqlserver存储过程和C#分页类简化你的代码!_字段_126{return strGetFields ;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            setSqlserver存储过程和C#分页类简化你的代码!_字段_126{strGetFields =value;}
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        private string fldName=string.Empty;
InBlock.gif        public string FldName
ExpandedSubBlockStart.gifContractedSubBlock.gif        Sqlserver存储过程和C#分页类简化你的代码!_字段_126{
ExpandedSubBlockStart.gifContractedSubBlock.gif            getSqlserver存储过程和C#分页类简化你的代码!_字段_126{return fldName ;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            setSqlserver存储过程和C#分页类简化你的代码!_字段_126{fldName =value;}
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        
InBlock.gif
InBlock.gif        private int pagesize =10;
InBlock.gif        public int PageSize
ExpandedSubBlockStart.gifContractedSubBlock.gif        Sqlserver存储过程和C#分页类简化你的代码!_字段_126{
ExpandedSubBlockStart.gifContractedSubBlock.gif            getSqlserver存储过程和C#分页类简化你的代码!_字段_126{return pagesize ;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            setSqlserver存储过程和C#分页类简化你的代码!_字段_126{pagesize =value;}
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        private int pageindex =1;
InBlock.gif        public int PageIndex
ExpandedSubBlockStart.gifContractedSubBlock.gif        Sqlserver存储过程和C#分页类简化你的代码!_字段_126{
ExpandedSubBlockStart.gifContractedSubBlock.gif            getSqlserver存储过程和C#分页类简化你的代码!_字段_126{return pageindex ;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            setSqlserver存储过程和C#分页类简化你的代码!_字段_126{pageindex =value;}
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif
InBlock.gif        private bool docount=false;
InBlock.gif        public bool DoCount
ExpandedSubBlockStart.gifContractedSubBlock.gif        Sqlserver存储过程和C#分页类简化你的代码!_字段_126{
ExpandedSubBlockStart.gifContractedSubBlock.gif            getSqlserver存储过程和C#分页类简化你的代码!_字段_126{return docount ;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            setSqlserver存储过程和C#分页类简化你的代码!_字段_126{docount =value;}
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        private bool ordertype=false;
InBlock.gif        public bool OrderType
ExpandedSubBlockStart.gifContractedSubBlock.gif        Sqlserver存储过程和C#分页类简化你的代码!_字段_126{
ExpandedSubBlockStart.gifContractedSubBlock.gif            getSqlserver存储过程和C#分页类简化你的代码!_字段_126{return ordertype ;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            setSqlserver存储过程和C#分页类简化你的代码!_字段_126{ordertype =value;}
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        private string strwhere=string.Empty ;
InBlock.gif        public string StrWhere
ExpandedSubBlockStart.gifContractedSubBlock.gif        Sqlserver存储过程和C#分页类简化你的代码!_字段_126{
ExpandedSubBlockStart.gifContractedSubBlock.gif            getSqlserver存储过程和C#分页类简化你的代码!_字段_126{return strwhere ;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            setSqlserver存储过程和C#分页类简化你的代码!_字段_126{strwhere =value;}
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        
InBlock.gif
InBlock.gif
InBlock.gif    
InBlock.gif
InBlock.gif        public IDataReader GetDataReader()
ExpandedSubBlockStart.gifContractedSubBlock.gif        Sqlserver存储过程和C#分页类简化你的代码!_字段_126{
InBlock.gif
InBlock.gif            if(this.docount)
ExpandedSubBlockStart.gifContractedSubBlock.gif            Sqlserver存储过程和C#分页类简化你的代码!_字段_126{
InBlock.gif                throw new ArgumentException("要返回记录集,DoCount属性一定为false");
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif        
InBlock.gif
InBlock.gif        //    System.Web.HttpContext.Current.Response.Write(pageindex);
InBlock.gif
InBlock.gif            return SqlHelper.ExecuteReader(connectionString,CommandType.StoredProcedure,"Pagination",
InBlock.gif                new SqlParameter("@tblName",this.tblName),
InBlock.gif                new SqlParameter("@strGetFields",this.strGetFields),
InBlock.gif                new SqlParameter("@fldName",this.fldName),
InBlock.gif                new SqlParameter("@PageSize",this.pagesize),
InBlock.gif                new SqlParameter("@PageIndex",this.pageindex),
InBlock.gif                new SqlParameter("@doCount",this.docount),
InBlock.gif                new SqlParameter("@OrderType",this.ordertype),
InBlock.gif                new SqlParameter("@strWhere",this.strwhere)
InBlock.gif                );
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        public DataSet GetDataSet()
ExpandedSubBlockStart.gifContractedSubBlock.gif        Sqlserver存储过程和C#分页类简化你的代码!_字段_126{
InBlock.gif            if(this.docount)
ExpandedSubBlockStart.gifContractedSubBlock.gif            Sqlserver存储过程和C#分页类简化你的代码!_字段_126{
InBlock.gif                throw new ArgumentException("要返回记录集,DoCount属性一定为false");
ExpandedSubBlockEnd.gif            }    
InBlock.gif
InBlock.gif            return SqlHelper.ExecuteDataset(connectionString,CommandType.StoredProcedure,"Pagination",
InBlock.gif                new SqlParameter("@tblName",this.tblName),
InBlock.gif                new SqlParameter("@strGetFields",this.strGetFields),
InBlock.gif                new SqlParameter("@fldName",this.fldName),
InBlock.gif                new SqlParameter("@PageSize",this.pagesize),
InBlock.gif                new SqlParameter("@PageIndex",this.pageindex),
InBlock.gif                new SqlParameter("@doCount",this.docount),
InBlock.gif                new SqlParameter("@OrderType",this.ordertype),
InBlock.gif                new SqlParameter("@strWhere",this.strwhere)
InBlock.gif                );
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif
InBlock.gif        public int GetCount()
ExpandedSubBlockStart.gifContractedSubBlock.gif        Sqlserver存储过程和C#分页类简化你的代码!_字段_126{
InBlock.gif            if(!this.docount)
ExpandedSubBlockStart.gifContractedSubBlock.gif            Sqlserver存储过程和C#分页类简化你的代码!_字段_126{
InBlock.gif                throw new ArgumentException("要返回总数统计,DoCount属性一定为true");
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif                    
InBlock.gif
InBlock.gif            return (int)SqlHelper.ExecuteScalar(connectionString,CommandType.StoredProcedure,"Pagination",
InBlock.gif                new SqlParameter("@tblName",this.tblName),
InBlock.gif                new SqlParameter("@strGetFields",this.strGetFields),
InBlock.gif                new SqlParameter("@fldName",this.fldName),
InBlock.gif                new SqlParameter("@PageSize",this.pagesize),
InBlock.gif                new SqlParameter("@PageIndex",this.pageindex),
InBlock.gif                new SqlParameter("@doCount",this.docount),
InBlock.gif                new SqlParameter("@OrderType",this.ordertype),
InBlock.gif                new SqlParameter("@strWhere",this.strwhere)
InBlock.gif                );
ExpandedSubBlockEnd.gif        }        
InBlock.gif
ExpandedSubBlockEnd.gif    }
InBlock.gif
InBlock.gif    
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}
None.gif

 

如何调用???

假如我已经建立了2个类。一个是FavList数据库实体类,一个FavListCollection集合类。FavListCollection存储了FavList实体类的集合。

我可以这样写一个方法。

ExpandedBlockStart.gifContractedBlock.gif /**//// <summary>
InBlock.gif        /// 返回FavList集合,使用存储过程自定义分页。
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="userid">数据库FavList的字段,用户ID</param>
InBlock.gif        /// <param name="strwhere">查找的条件</param>
InBlock.gif        /// <param name="ordertype">排序,true表示Desc,false表示asc</param>
InBlock.gif        /// <param name="fldname">排序的字段,只能是一个字段</param>
InBlock.gif        /// <param name="pagesize">每页的记录数</param>
InBlock.gif        /// <param name="pageindex">到第几页的参数,由1开始。1表示第一页,以此类推。</param>
InBlock.gif        /// <param name="recordcount">总记录数。</param>
ExpandedBlockEnd.gif        /// <returns></returns>
None.gif        public override FavListCollection GetFavListsByUser(int userid, string strwhere, 
None.gif            bool ordertype, string fldname, int pagesize,
None.gif            int pageindex,out int recordcount
None.gif            )
ExpandedBlockStart.gifContractedBlock.gif        Sqlserver存储过程和C#分页类简化你的代码!_字段_126{
InBlock.gif            recordcount = 0;
InBlock.gif            PagerHelper helper = new PagerHelper("Vfavlist",strwhere,ConnectionString); //VFavList是View
InBlock.gif            recordcount = helper.GetCount();
InBlock.gif            
InBlock.gif            PagerHelper helper2 = new PagerHelper("Vfavlist",false," * ",fldname,
InBlock.gif                pagesize,pageindex,ordertype,strwhere,ConnectionString);
InBlock.gif
InBlock.gif            IDataReader dr = helper2.GetDataReader();
InBlock.gif
InBlock.gif            FavListCollection list = new FavListCollection();
InBlock.gif        
InBlock.gif            while(dr.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif            Sqlserver存储过程和C#分页类简化你的代码!_字段_126{
InBlock.gif                list.Add(PopulateFavList(dr));
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif            dr.Close();
InBlock.gif
InBlock.gif            return list;
ExpandedBlockEnd.gif        }