博客
关于我
前端分页、及分页原理
阅读量:412 次
发布时间:2019-03-06

本文共 11222 字,大约阅读时间需要 37 分钟。

分页原理:越过多少条。取多少条

1         ///  2         /// 分页嵌套查询 3         ///  4         /// 查询SQL语句 5         /// 降序字段eg:order by id/order by id desc 6         /// 开始 7         /// 一页多少条 8         /// 总条数 9         /// 
返回DataTable
10 public static DataTable QueryDT(string strSql,string orderBy,int start,int limit,ref int total)11 {12 using (IDbConnection conn = defaultPro.GetConnection())13 {14 if (conn.State != ConnectionState.Open)15 {16 conn.Open();17 }18 try19 {20 string query_sql = string.Format(@"SELECT CSON4.* FROM ( SELECT CSON3.* FROM (21 Select CSON2.*, ROW_NUMBER() OVER({1}) as rownum from (22 Select Count(*) over() total_count,CSON.* From ({0}) CSON23 ) CSON2 24 ) CSON3 where CSON3.rownum<{3}) CSON4 WHERE CSON4.rownum>{2}", strSql, orderBy, (start - 1) * limit, start * limit + 1);25 PrintErrorStrSql(strSql);26 IDbDataAdapter adap = defaultPro.GetDataAdapter(strSql, conn);27 DataTable dt = new DataTable();28 DataSet ds = new DataSet();29 adap.Fill(ds);30 dt = ds.Tables[0];31 if (dt.Rows.Count>0)32 {33 total =Convert.ToInt32(dt.Rows[0]["total_count"]);34 } 35 return dt;36 }37 catch (DbException ex)38 {39 throw new Exception(ex.Message);40 }41 finally42 {43 conn.Close();44 }45 }46 }
View Code
1 SELECT TOP (30) TMP.* FROM (SELECT *,ROW_NUMBER() OVER(ORDER BY ID) rn FROM UserInfo) TMP WHERE rn>30  ORDER BY ID DESC2 3 4 SELECT TOP (前多少行) TMP.* FROM (SELECT *,ROW_NUMBER() OVER(ORDER BY 排序字段) rn FROM 表名) TMP WHERE rn>越过多少行   ORDER BY 排序字段 DESC
微软标准分页查询
1     SELECT TOP(5)* FROM Main WHERE id not in2     (3         SELECT TOP(5*2) id FROM Main ORDER BY id4     )5     ORDER BY ID
View Code
1         ///  2         /// 分页 3         ///  4         /// 一页多少条 5         /// 当前页的索引 6         /// 总条数 7         /// 
8 public static string ShowPageNavigate(int pageSize,int currentPageIndex,int totalCount) 9 {10 pageSize = pageSize == 0 ? 3 : pageSize;11 var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数12 var output = new StringBuilder();13 if (totalPages>0)14 {15 if (currentPageIndex != 1)16 { //处理首页链接17 output.AppendFormat("首页",pageSize);18 }19 if (currentPageIndex > 1)20 {//处理上一页的链接21 output.AppendFormat("上一页", currentPageIndex - 1,pageSize);22 }23 output.Append(" ");24 int currint = 5;25 for (int i = 0; i <=10; i++)26 {//一共最多显示10个页面,前面5个,后面5个27 if (currentPageIndex + i - currint > totalPages) //处理总页数少于10页28 {29 break;30 }31 if ((currentPageIndex + i-currint)>=1&&(currentPageIndex + i-currint)<=totalCount)32 {33 if (currint == i)34 { //当前页处理35 output.AppendFormat("{2}", currentPageIndex, pageSize, currentPageIndex);36 }37 else38 {//一般页处理39 output.AppendFormat("{2}", currentPageIndex + i-currint,pageSize, currentPageIndex + i-currint);40 }41 }42 output.Append(" ");43 }44 if (currentPageIndex < totalPages)45 {//处理下一页的链接46 output.AppendFormat("下一页", currentPageIndex + 1,pageSize);47 }48 output.Append(" ");49 if (currentPageIndex != totalPages)50 {51 output.AppendFormat("末页",totalPages,pageSize);52 }53 }54 output.AppendFormat("第{0}页/共{1}页", currentPageIndex, totalPages); //统计页数55 return output.ToString();56 }
1         a { 2             text-decoration: none; 3         } 4  5         .ep-pages { 6             padding: 10px 12px; 7             clear: both; 8             font-family: Arial, "\5B8B\4F53", sans-serif; 9             font-size: 14px;10             vertical-align: top;11         }12 13         .ep-pages a, .ep-pages span {14             display: inline-block;15             height: 23px;16             line-height: 23px;17             padding: 0 8px;18             margin: 5px 1px 0 0;19             background: #fff;20             border: 1px solid #e5e5e5;21             overflow: hidden;22             vertical-align: top;23         }24 25         .ep-pages a:hover {26             background: #cc1b1b;27             border: 1px solid #cc1b1b;28             text-decoration: none;29         }30 31         .ep-pages a, .ep-pages a:visited {32             color: #252525;33         }34 35         .ep-pages a:hover, .ep-pages a:active {36             color: #ffffff;37         }38 39         .ep-pages .current {40             background: #cc1b1b;41             border: 1px solid #cc1b1b;42             color: #fff;43         }44 45         .ep-pages a.current, .ep-pages a.current:visited {46             color: #ffffff;47         }48 49         .ep-pages a.current:hover, .ep-pages a.current:active {50             color: #ffffff;51         }52 53         .ep-pages-ctrl {54             font-family: "\5B8B\4F53", sans-serif;55             font-weight: bold;56             font-size: 16px;57         }58 59         .ep-pages-e5e5e5 {60             color: #e5e5e5;61         }62 63         .ep-pages-all {64             font-size: 12px;65             vertical-align: top;66         }
分页CSS样式

使用示例

 

效果图:

 

分页SQL语句

1 SELECT * FROM (select ROW_NUMBER() over (order by id) as row,TT.* from Main TT) TTT2 WHERE TTT.row BETWEEN 5 AND 103 4 模板:5 SELECT * FROM (select ROW_NUMBER() over (order by 排序字段) as row,TT.* from 表 TT) TTT6 WHERE TTT.row BETWEEN 第几条 AND 第几条

效果图

 存储过程分页

 创建存储过程

1 create proc P_LoadPageData 2     @pageIndex int, 3     @pageSize int, 4     @total int out 5 as 6 begin 7 --分页原理:越过多少条。取多少条 8     SELECT TOP(@pageIndex)* FROM Main WHERE id not in 9     (10         SELECT TOP((@pageIndex-1)*@pageSize) id FROM Main ORDER BY id11     )12     ORDER BY ID13     SELECT @total=COUNT(1) FROM Main14     SELECT @total15 end

测试刚才写的存储过程

1 create proc P_LoadPageData 2     @pageIndex int, 3     @pageSize int, 4     @total int out 5 as 6 begin 7 --分页原理:越过多少条。取多少条 8     SELECT TOP(@pageIndex)* FROM Main WHERE id not in 9     (10         SELECT TOP((@pageIndex-1)*@pageSize) id FROM Main ORDER BY id11     )12     ORDER BY ID13     SELECT @total=COUNT(1) FROM Main14     SELECT @total15 end16 17 --存储过程测试18 declare @total int19 exec P_LoadPageData 3,5,@total20 print @total

效果图

 

程序调用

1         public List
LoadPageData(int pageIndex, int pageSize, out int total) 2 { 3 SqlParameter tal = new SqlParameter("@total", SqlDbType.Int); 4 tal.Direction = ParameterDirection.Output; //设置为输出参数 5 SqlParameter[] pms = new SqlParameter[] { 6 new SqlParameter("@pageIndex",SqlDbType.Int) {Value=pageIndex }, 7 new SqlParameter("@pageSize",SqlDbType.Int) {Value=pageSize }, 8 tal 9 };10 11 DataSet ds= SqlHelper.GetList("P_LoadPageData", CommandType.StoredProcedure, pms);12 total = (int)tal.Value; //拿到输出参数的值13 DataTable dt = ds.Tables[0];14 return Common.ToEntity.DtConvertToModel
(dt);15 }
1         ///  2         /// 执行sql语句或存储过程,返回DataSet 3         ///  4         /// 存储过程名称/sql语句 5         /// 执行类型 6         /// 可变参数 7         /// 
8 public static DataSet GetList(string procNameOrStrSql,CommandType cmdStoredProcedure, SqlParameter[] pms) 9 {10 try11 {12 13 using (SqlConnection conn=new SqlConnection(connStr))14 {15 using (SqlDataAdapter adap = new SqlDataAdapter(procNameOrStrSql, conn))16 {17 DataSet ds = new DataSet();18 //添加参数19 if (pms != null)20 {21 adap.SelectCommand.Parameters.AddRange(pms);22 }23 adap.SelectCommand.CommandType = cmdStoredProcedure;24 adap.Fill(ds);25 return ds;26 }27 }28 }29 catch (Exception ex)30 {31 WriteLog(procNameOrStrSql, ex);32 throw new Exception("错误内容:" + ex.Message.ToString());33 }34 }
sqlHelper类
1         ///  2         /// 将DataTable转换成实体类 3         ///  4         /// 
实体类
5 /// DataTable 6 ///
7 public static List
DtConvertToModel
(DataTable dt) where T:new() 8 { 9 List
ts = new List
();10 foreach (DataRow dr in dt.Rows)11 {12 T t = new T();13 foreach (PropertyInfo pi in t.GetType().GetProperties())14 {15 if (dt.Columns.Contains(pi.Name))16 {17 if (!pi.CanWrite) continue;18 var value = dr[pi.Name];19 if (value!= DBNull.Value)20 {21 switch (pi.PropertyType.FullName)22 {23 case "System.Decimal":24 pi.SetValue(t, decimal.Parse(value.ToString()), null);25 break;26 case "System.String":27 pi.SetValue(t, value.ToString(), null);28 break;29 case "System.Int32":30 pi.SetValue(t, int.Parse(value.ToString()), null);31 break;32 default:33 pi.SetValue(t, value, null);34 break;35 }36 }37 } 38 }39 ts.Add(t);40 }41 return ts;42 }
DataTable反射实体类

转载地址:http://jwmkz.baihongyu.com/

你可能感兴趣的文章
mysql 四种存储引擎
查看>>
MySQL 基础模块的面试题总结
查看>>
MySQL 备份 Xtrabackup
查看>>
mysql 多个表关联查询查询时间长的问题
查看>>
mySQL 多个表求多个count
查看>>
mysql 多字段删除重复数据,保留最小id数据
查看>>
MySQL 多表联合查询:UNION 和 JOIN 分析
查看>>
MySQL 大数据量快速插入方法和语句优化
查看>>
mysql 如何给SQL添加索引
查看>>
mysql 字段区分大小写
查看>>
mysql 字段合并问题(group_concat)
查看>>
mysql 字段类型类型
查看>>
MySQL 字符串截取函数,字段截取,字符串截取
查看>>
MySQL 存储引擎
查看>>
mysql 存储过程 注入_mysql 视图 事务 存储过程 SQL注入
查看>>
MySQL 存储过程参数:in、out、inout
查看>>
mysql 存储过程每隔一段时间执行一次
查看>>
mysql 存在update不存在insert
查看>>
Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
查看>>
Mysql 学习总结(87)—— Mysql 执行计划(Explain)再总结
查看>>