用Moon.Orm来做分页数据显示
小弟来献丑了!最近弄一个自己的项目,底层使用Moon.Orm。有兴趣的可以看http://www.cnblogs.com/humble/p/3312018.html。
至于好不好我说了不算,但是我自己用得还是蛮爽的。话不多说贴上关键代码
1.现实基于sql的分页方法,网上有很多高效分页预计,我只是选择一个比较合适的
CREATE PROCEDURE [dbo].[proc_DataPagingList] ( @tableName NVARCHAR(200), ----要显示的表或多个表的连接 @fieldNames NVARCHAR(200)='*', ----要显示的字段列表 @pageSize INT = 10, ----每页显示的记录个数 @page INT = 10, ----要显示那一页的记录 @pageCount INT = 1 output, ----查询结果分页后的总页数 @counts INT = 1 output, ----查询到的总记录数 @fieldSort NVARCHAR(200)= null,----排序字段列表或条件 @sort BIT = 1, ----排序方法,0为升序,1为降序--程序传参如:' SortA Asc,SortB Desc,SortC ') @condition NVARCHAR(200)= null,----查询条件,不需WHERE @keyID NVARCHAR(100), ----主表的主键 @distinct BIT = 0 ----是否添加查询字段的 DISTINCT 默认0不添加/1添加 ) AS SET NOCOUNT ON Declare @SELECT NVARCHAR(1000) ----存放动态生成的SQL语句 Declare @strCounts NVARCHAR(1000) ----存放取得查询结果总数的查询语句 Declare @strID NVARCHAR(1000) ----存放取得查询开头或结尾ID的查询语句 Declare @sortTypeA NVARCHAR(10) ----数据排序规则A Declare @SortTypeB NVARCHAR(10) ----数据排序规则B Declare @distSelect NVARCHAR(50) ----对含有DISTINCT的查询进行SQL构造 Declare @distCounts NVARCHAR(50) ----对含有DISTINCT的总数查询进行SQL构造 DECLARE @SortfieldA NVARCHAR(50) ----对含有是否还有排序字段时的排序方式组合A DECLARE @SortfieldB NVARCHAR(50) ----对含有是否还有排序字段时的排序方式组合B IF @distinct = 0 BEGIN SET @distSelect = 'SELECT ' SET @distCounts = ' COUNT(*)' END ELSE BEGIN SET @distSelect = 'SELECT distinct ' SET @distCounts = ' COUNT(DISTINCT '+@keyID+')' END IF @sort=0 BEGIN SET @SortTypeB=' ASC ' SET @sortTypeA=' DESC ' END ELSE BEGIN SET @SortTypeB=' DESC ' SET @sortTypeA=' ASC ' END IF @fieldSort IS NOT NULL AND @fieldSort<>'' --排序字段不为空时 BEGIN SET @SortfieldB=' order by '+ @fieldSort +' '+ @SortTypeB SET @SortfieldA=' order by '+ @fieldSort +' '+ @SortTypeA END ELSE BEGIN SET @SortfieldB='' SET @SortfieldA='' END --------生成查询语句-------- --此处@strCounts为取得查询结果数量的语句 IF @condition is null or @condition='' --没有设置显示条件 BEGIN SET @SELECT = @fieldNames + ' FROM ' + @tableName SET @strCounts = @distSelect+' @counts='+@distCounts+' FROM '+@tableName SET @strID = ' FROM ' + @tableName END ELSE BEGIN SET @SELECT = + @fieldNames + 'FROM ' + @tableName + ' WHERE ' + @condition SET @strCounts = @distSelect+' @counts='+@distCounts+' FROM '+@tableName + ' WHERE ' + @condition SET @strID = ' FROM ' + @tableName + ' WHERE ' + @condition END ----取得查询结果总数量----- exec sp_executesql @strCounts,N'@counts INT out ',@counts out DECLARE @tmpCounts INT IF @counts = 0 SET @tmpCounts = 1 ELSE SET @tmpCounts = @counts --取得分页总数 SET @pageCount=(@tmpCounts+@pageSize-1)/@pageSize --/**当前页大于总页数 取最后一页**/ IF @page>@pageCount SET @page=@pageCount --/*-----数据分页2分处理-------*/ DECLARE @pageIndex INT --总数/页大小 DECLARE @lastcount INT --总数%页大小 SET @pageIndex = @tmpCounts/@pageSize SET @lastcount = @tmpCounts%@pageSize IF @lastcount > 0 SET @pageIndex = @pageIndex + 1 ELSE SET @lastcount = @pageSize --显示分页 IF @condition is null or @condition='' --没有设置显示条件 BEGIN IF @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分数据处理 BEGIN SET @strCounts=@distSelect+' TOP '+ CAST(@pageSize as VARCHAR(4))+' '+ @fieldNames+' FROM '+@tableName + ' WHERE '+@keyID+' not in('+ @distSelect+' TOP '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @keyID +' FROM '+@tableName + @SortfieldB +')' + @SortfieldB END ELSE BEGIN SET @page = @pageIndex-@page+1 --后半部分数据处理 IF @page <= 1 --最后一页数据显示 SET @strCounts=@distSelect+' * FROM ('+@distSelect+' TOP '+ CAST(@lastcount as VARCHAR(4))+' '+ @fieldNames+' FROM '+@tableName + @SortfieldA+') AS TempTB '+@SortfieldB ELSE SET @strCounts=@distSelect+' * FROM ('+@distSelect+' TOP '+ CAST(@pageSize as VARCHAR(4))+' '+ @fieldNames+' FROM '+@tableName + ' WHERE '+@keyID+' not in('+ @distSelect+' TOP '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @keyID +' FROM '+@tableName + @SortfieldA+')' + @SortfieldA+') AS TempTB '+@SortfieldB END END ELSE --有查询条件 BEGIN IF @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分数据处理 BEGIN SET @strCounts=@distSelect+' TOP '+ CAST(@pageSize as VARCHAR(4))+' '+ @fieldNames +' FROM '+@tableName + ' WHERE '+@keyID+' not in('+ @distSelect+' TOP '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @keyID +' FROM '+@tableName + ' Where ' + @condition + @SortfieldB+')'+' AND ' + @condition + @SortfieldB END ELSE BEGIN SET @page = @pageIndex-@page+1 --后半部分数据处理 IF @page <= 1 --最后一页数据显示 SET @strCounts=@distSelect+' * FROM ('+@distSelect+' TOP '+ CAST(@lastcount as VARCHAR(4))+' '+ @fieldNames+' FROM '+@tableName + ' WHERE '+ @condition +@SortfieldA+') AS TempTB '+@SortfieldB ELSE SET @strCounts=@distSelect+' * FROM ('+@distSelect+' TOP '+ CAST(@pageSize as VARCHAR(4))+' '+ @fieldNames+' FROM '+@tableName + ' WHERE '+@keyID+' not in('+ @distSelect+' TOP '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' ' + @keyID +' FROM '+@tableName +' WHERE '+ @condition +@SortfieldA+')' +' AND '+ @condition +@SortfieldA+') AS TempTB ' + @SortfieldB END END ------返回查询结果----- exec sp_executesql @strCounts SET NOCOUNT OFF
2.这是基于Moon.Orm的基类我拿来加了一个自己方法里面来用,
主要是看使用了ParameterDirection.Output来调取存储过程的返回值,就是总页数和每页显示的页数。
public List<T2> GetPagingData<T2>(PageClass pc) where T2 : new() { DbParameter[] parameters = { new SqlParameter("@tableName", SqlDbType.NVarChar,200) , new SqlParameter("@fieldNames", SqlDbType.NVarChar,200) , new SqlParameter("@pageSize", SqlDbType.Int,4) , new SqlParameter("@page", SqlDbType.Int,4) , new SqlParameter("@pageCount", SqlDbType.Int,4 ) , new SqlParameter("@counts", SqlDbType.Int,4) , new SqlParameter("@fieldSort", SqlDbType.NVarChar,200) , new SqlParameter("@sort", SqlDbType.Bit,1) , new SqlParameter("@condition", SqlDbType.NVarChar,200) , new SqlParameter("@keyID", SqlDbType.NVarChar,100) , new SqlParameter("@distinct", SqlDbType.Bit,1) }; parameters[0].Value = pc.sys_Table; parameters[1].Value = pc.sys_Fields; parameters[2].Value = pc.sys_PageSize; parameters[3].Value = pc.sys_PageIndex; parameters[4].Direction = ParameterDirection.Output; parameters[5].Direction = ParameterDirection.Output; parameters[6].Value = null; parameters[7].Value = 1; parameters[8].Value = pc.sys_Where; parameters[9].Value = pc.sys_Key; parameters[10].Value = 0; using (var db = Db.CreateDefaultDb()) { // db.DebugEnabled = true; List<T2> list= db.ExecuteProToOwnList<T2>("proc_DataPagingList", parameters); pc.RCount = Convert.ToInt32(parameters[5].Value); pc.PCount = Convert.ToInt32(parameters[4].Value); return list; } }
3.这是我弄的一个分页帮助类,方便赋值而已
public class PageClass { /// <summary> /// 总页数输出 /// </summary> public int PCount { get; set; } // -- /// <summary> /// 总记录数输出 /// </summary> public int RCount { get; set; } //-- /// <summary> /// 查询表名 /// </summary> public string sys_Table { get; set; } // -- /// <summary> /// 主键 /// </summary> public string sys_Key { get; set; } // -- /// <summary> /// 查询字段 /// </summary> public string sys_Fields { get; set; } // -- /// <summary> /// 查询条件 /// </summary> public string sys_Where { get; set; } // -- /// <summary> /// 排序字段 /// </summary> public string sys_Order { get; set; } //-- /// <summary> /// 开始位置 /// </summary> public int sys_Begin { get; set; } //-- /// <summary> /// 当前页数 /// </summary> public int sys_PageIndex { get; set; } // -- /// <summary> /// 页大小 /// </summary> public int sys_PageSize { get; set; } // -- }
最后个人使用感觉Moon.Orm速度很快,但是有些还有很有特点的语法,感兴趣可以去看看。暂时写了sql的分页,有些时间弄一些别的数据库的,欢迎大家赐教。不喜勿喷!谢谢