Entity FrameWork 实现分页
SQl语句进行分页
SQL语句进行分页主要是应用Entity FrameWork的SqlQuery()传入SQL语句进行查询时分页。
效果展示。

页面代码展示,显示是用Repeater控件进行动态显示
<%@ Page Title="" Language="C#" MasterPageFile="~/HoTai/HoTaiGuanLi.Master" AutoEventWireup="true" CodeBehind="HoTaiYingYYM.aspx.cs" Inherits="OLMS.HoTai.HoTaiYingYYM" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<link href="../Assets/css/input.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div class="pure-form">
<fieldset>
<legend>后台管理<i class="fa fa-angle-double-right"></i>类型管理
<asp:Button ID="Button1" CssClass=" pure-button pure-button-primary tools-button" runat="server" Text="添加音乐类型" OnClick="Button1_Click" />
</legend>
</fieldset>
</div>
<div>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">编号</th>
<th scope="col">应用名称</th>
<th scope="col">类型名称</th>
<th scope="col">歌手名称</th>
<th scope="col">歌手价格</th>
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<tr>
<th scope="row"><%# Eval("AlbumId") %></th> //表的id
<td><%# Eval("Title") %></td>
<td><%# Eval("Genres.Name") %></td>
<td><%# Eval("Artists.Name") %></td>
<td><%# Eval("Price") %></td>
<td>
<asp:LinkButton ID="LinkButton1" CssClass="btn btn-primary" CommandArgument='<%# Eval("AlbumId") %>' CommandName="dianjia" runat="server">编辑</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" CssClass="btn btn-light" CommandArgument='<%# Eval("AlbumId") %>' CommandName="delete" runat="server">删除</asp:LinkButton></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
</div>
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button2" runat="server" Text="首页" OnClick="Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="上一页" OnClick="Button3_Click" />
<asp:Button ID="Button4" runat="server" Text="下一页" OnClick="Button4_Click" />
<asp:Button ID="Button5" runat="server" Text="末页" OnClick="Button5_Click" />
</div>
</asp:Content>
后台事件代码
using OLMS.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace OLMS.HoTai
{
public partial class HoTaiYingYYM : System.Web.UI.Page
{
static int x = 0;
static int i = 1;
static int zyes;
static int ztian;
static int tianos = 5;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//刷新
Select();
}
}
private void Select()
{
using (OLMSDBEntities db = new OLMSDBEntities())
{
string sql = $"select * from Albums order by AlbumId offset {x} rows fetch next 5 rows only";
Repeater1.DataSource = db.Albums.SqlQuery(sql).ToList();
Repeater1.DataBind();
var fy = db.Albums.ToList();
ztian = fy.Count;
zyes = ztian / tianos;
Label1.Text = $"每页5条/共{ztian}条 第{i}页/共{zyes + 1}页";
}
}
protected void Button2_Click(object sender, EventArgs e)
{
//首页
x = 0;
i = 1;
Select();
}
protected void Button4_Click(object sender, EventArgs e)
{
//下一页
x += 5;
i++;
if (i >= zyes)
{
x = zyes * 5;
i = zyes + 1;
}
if (i >= (zyes + 1))
{
string strUrl = "<script>alert('已到末页');</script>";
Response.Write(strUrl);
}
else
{
Select();
}
}
protected void Button3_Click(object sender, EventArgs e)
{
//上一页
x -= 5;
i--;
if (x < 0)
{
x = 0;
i = 1;
}
if (x <= 0)
{
string strUrl = "<script>alert('已到第一页');</script>";
Response.Write(strUrl);
}
else
{
Select();
}
}
protected void Button5_Click(object sender, EventArgs e)
{
//末页
x = zyes * 5;
i = zyes + 1;
Select();
}
}
}
Skip().Take()进行分页
Skip().Take()进行分页是应用Entity FrameWork的Skip(起始行).Take(每页多少行)方法来进行分页

页面结构和上面是一样的,后台事件代码有区别
using OLMS.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace OLMS.HoTai
{
public partial class HoTaiYingYYM : System.Web.UI.Page
{
static int i = 1; //起始页数
static int zyes; //总条数
static int ztian;//总页数
static int tianos = 5; //每页多少条
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//刷新
Select();
}
}
private void Select()
{
using (OLMSDBEntities db = new OLMSDBEntities())
{
var fy = db.Albums.ToList();
ztian = fy.Count;
zyes = ztian / tianos;
//起始行数 每页多少条
Repeater1.DataSource = db.Albums.ToList().Skip((i - 1) * tianos).Take(tianos).ToList();
Repeater1.DataBind();
Label1.Text = $"每页{tianos}条/共{fy.Count}条 第{i}页/共{(fy.Count / 5) + 1}页";
}
}
protected void Button2_Click(object sender, EventArgs e)
{
i = 1;
Select();
}
protected void Button4_Click(object sender, EventArgs e)
{
i++;
if (i >= zyes)
{
i = zyes + 1;
}
if (i >= (zyes + 1))
{
string strUrl = "<script>alert('已到末页');</script>";
Response.Write(strUrl);
}
else
{
Select();
}
}
protected void Button3_Click(object sender, EventArgs e)
{
i--;
if (i < 0)
{
i = 1;
}
if (i <= 0)
{
string strUrl = "<script>alert('已到第一页');</script>";
Response.Write(strUrl);
}
else
{
Select();
}
}
protected void Button5_Click(object sender, EventArgs e)
{
i = zyes + 1;
Select();
}
}
}
总结
Skip().Take()进行分页方法和SQl语句进行分页总体代码差不太多,按具体需要来进行选择。
Entity FrameWork 实现分页的更多相关文章
- entity framework 新手入门篇(3)-entity framework实现orderby,count,groupby,like,in,分页等
前面我们已经学习了entityframework的基本的增删改查,今天,我们将在EF中实现一些更加贴近于实际功能的SQL方法. 承接上面的部分,我们有一个叫做House的数据库,其中包含house表和 ...
- Entity Framework后台采用分页方式取数据与AspNetPager控件的使用
本文是一个对AspNetPager控件使用的笔记! 有关AspNetPager控件可以查看杨涛主页.这是一个开放的自定义ASP.NET控件,支持各种自定义的数据分页方式,使用很方便,而且功能也很强大, ...
- 基于Entity Framework的自定义分页,增删改的通用实现
简介 之前写个一个基于Dapper的分页实现,现在再来写一个基于Entity Framework的分页实现,以及增删改的通用实现. 代码 还是先上代码:https://github.com/jinwe ...
- Entity Framework分页扩展
Entity Framework分页在我初入门时总是困扰这我,无论是SQL分页还是Entity Framework的分页,总是显得那么麻烦,因此对于Entity Framework单独封装了分页. 一 ...
- 《Entity Framework 6 Recipes》中文翻译系列 (17) -----第三章 查询之分页、过滤和使用DateTime中的日期部分分组
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 3-12 分页和过滤 问题 你想使用分页和过滤来创建查询. 解决方案 假设你有如图3 ...
- [转]在Entity Framework中使用LINQ语句分页
本文转自:http://diaosbook.com/Post/2012/9/21/linq-paging-in-entity-framework 我们知道,内存分页效率很低.并且,如果是WebForm ...
- Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 排序、筛选、分页以及分组
Sorting, filtering, paging, and grouping 7 of 8 people found this helpful By Tom Dykstra The Contoso ...
- Entity Framework 学习之--Ling to entity实现分页
最近用MVC做的一个项目涉及到分页,中间用了entity framework来查数据库,不用直接写sql语句,方便了很多. 一般分页的思路是获得两个变量的值: 1.一共有多少条记录 totalCoun ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第五章:排序、分页和路由
本章的重点是对产品信息增加排序和分页的功能,以及使用ASP.NET Routing特性添加更加友好的URL支持. 注意:如果你想按照本章的代码编写示例,你必须完成第四章或者直接从www.apress. ...
随机推荐
- 201771030129-张琳 实验一软件工程准备—<阅读书之后的三个疑问>
项目 内容 课程班级博客链接 https://edu.cnblogs.com/campus/xbsf/nwnu2020SE/ 本次作业要求链接 https://www.cnblogs.com/nwnu ...
- 使用ramdisk启动ubuntu文件系统
环境 Qemu 4.1 vexpress-ca9 概述 为了减小linux内核的大小,可以把一些外设驱动编译成内核模块,但是在启动ubuntu的时候,需要读取flash,但是此时flash的驱动模块存 ...
- python --分隔符split()
描述: python split()是通过指定分隔符对字符串进行切片,且可以指定分隔n+1个字符串. 语法: str.split(str="",num=string.count(s ...
- Python --表达式和运算符
表达式 由一个或者几个数字或者变量和运算符组合成的一行代码 通常会返回一个结果 运算符 由一个以上的值经过变化得到新值的过程就叫做运算 用于运算的符号称为运算符 运算符的分类: 算数运算符 比较或者关 ...
- fakebook
0x01 查看robots.txt 发现user.php.bak文件 得到源码 <?php class UserInfo { public $name = ""; publi ...
- 简单而面试中又常见的知识点:JS执行机制
在开始讲解之前,我们先来看一段代码: console.log('1'); setTimeout(function() { console.log('2'); process.nextTick( ...
- Spring初学笔记(二):Bean的注入
关于Bean的注入 在上一篇中,已经说到虽然注入确实可以降低类与类之间的耦合,但并没有解决调用者必须知道类的创建方法的问题,也可以说是没有实现调用者与类实现的解耦,我们也提到,为了实现两者的解耦,可以 ...
- css中height, width默认值
转载自:https://www.cnblogs.com/heyode/p/5973960.html <body> <div class="wrap"> &l ...
- MySQL的列约束
1.列约束 (1)主键约束——PRIMARY KEY (2)非空约束——NOT NULL 声明了非空约束的列上,不允许使用NULL (3)唯一约束——UNIQUE 声明了唯一约束的列上不能插入重复的值 ...
- mvc 页面上循环datatable
@using System.Data; @{ Layout = null; } @{ DataTable DataServiceStaff = ViewBag.ServiceStaff as Data ...