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 实现分页的更多相关文章

  1. entity framework 新手入门篇(3)-entity framework实现orderby,count,groupby,like,in,分页等

    前面我们已经学习了entityframework的基本的增删改查,今天,我们将在EF中实现一些更加贴近于实际功能的SQL方法. 承接上面的部分,我们有一个叫做House的数据库,其中包含house表和 ...

  2. Entity Framework后台采用分页方式取数据与AspNetPager控件的使用

    本文是一个对AspNetPager控件使用的笔记! 有关AspNetPager控件可以查看杨涛主页.这是一个开放的自定义ASP.NET控件,支持各种自定义的数据分页方式,使用很方便,而且功能也很强大, ...

  3. 基于Entity Framework的自定义分页,增删改的通用实现

    简介 之前写个一个基于Dapper的分页实现,现在再来写一个基于Entity Framework的分页实现,以及增删改的通用实现. 代码 还是先上代码:https://github.com/jinwe ...

  4. Entity Framework分页扩展

    Entity Framework分页在我初入门时总是困扰这我,无论是SQL分页还是Entity Framework的分页,总是显得那么麻烦,因此对于Entity Framework单独封装了分页. 一 ...

  5. 《Entity Framework 6 Recipes》中文翻译系列 (17) -----第三章 查询之分页、过滤和使用DateTime中的日期部分分组

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 3-12 分页和过滤 问题 你想使用分页和过滤来创建查询. 解决方案 假设你有如图3 ...

  6. [转]在Entity Framework中使用LINQ语句分页

    本文转自:http://diaosbook.com/Post/2012/9/21/linq-paging-in-entity-framework 我们知道,内存分页效率很低.并且,如果是WebForm ...

  7. 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 ...

  8. Entity Framework 学习之--Ling to entity实现分页

    最近用MVC做的一个项目涉及到分页,中间用了entity framework来查数据库,不用直接写sql语句,方便了很多. 一般分页的思路是获得两个变量的值: 1.一共有多少条记录 totalCoun ...

  9. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第五章:排序、分页和路由

    本章的重点是对产品信息增加排序和分页的功能,以及使用ASP.NET Routing特性添加更加友好的URL支持. 注意:如果你想按照本章的代码编写示例,你必须完成第四章或者直接从www.apress. ...

随机推荐

  1. Day_11【集合】扩展案例4_删除长度大于5的字符串,删除元素包含0-9数字的字符串

    分析以下需求,并用代码实现 1.定义ArrayList集合,存入多个字符串 如:"ab1" "123ad" "bca" "dadf ...

  2. ubuntu文件系统修改( for arm)

    系统:ubuntu14.04 镜像:ubuntu-rootfs.img for aarch64 创建一个文件夹 ubuntu-mount mkdir ubuntu-mount 将ubuntu-root ...

  3. ScrollView 内嵌百度地图问题解决

    在ScrollView上内嵌百度地图遇到两个问题 事件冲突,移动地图的时候屏幕滚动了 移动ScrollView的时候,百度地图出现黑边 问题1的处理就有各种办法了,核心都是拦截事件,我使用的办法是加一 ...

  4. 如何发布自己用python写的py模块

    Python——怎么发布你的Python模块 我们在学习Python的时候,除了用pip安装一些模块之外,有时候会从网站下载安装包下来安装,我也想要把我自己编写的模块做成这样的安装包,该怎么办,如何发 ...

  5. 走进WebApiClientCore的设计

    WebApiClient WebApiClient是NCC开源社区的一个项目,是目前微服务里http接口调用的一把锋利尖刀,项目早期设计与开发的时候,是基于.netframework的,然后慢慢加入n ...

  6. Redux:异步操作

    最近状态不太好,学习redux的异步操作花的时间比想象的多,这里尽量清晰简要的表述一下在redux中怎么实现异步操作. 先回顾一下同步操作: 我们用redux执行同步的时候,都是先发起一个dispat ...

  7. MyEclipse安装后的配置

    一.Window-->Preferences-->General --> Workspace --> UTF-8 作用:从此以后,你创建的任何项目编码都是UTF-8,一次解决所 ...

  8. 委托 事件 observer

    详细介绍http://www.cnblogs.com/jcz1206/articles/2730793.html  ---摘录别人的 using System;using System.Collect ...

  9. BZOJ 1084DP

    1084: [SCOI2005]最大子矩阵 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2796  Solved: 1391[Submit][Sta ...

  10. LightOJ1197

    题目链接:https://vjudge.net/problem/LightOJ-1197 题目大意: 给你 a 和 b (1 ≤ a ≤ b < 231, b - a ≤ 100000),求出 ...