zoukankan      html  css  js  c++  java
  • Dapper实现 Linq 和 存储过程 + JS 实现分页

    后台方法Linq

    using (SqlConnection conn = new SqlConnection(str))
    {
      string sql = $"select * from Goods where 1 = 1";
      if (!string.IsNullOrWhiteSpace(name))
      {
        sql += $"and Name like '%{name}%'";
      }
      var list = conn.Query<ModelInfo>(sql);
      Pages pag = new Pages();
      pag.ModelInfos = list.OrderBy(x => x.ID)
        .Skip((index - 1) * size)
        .Take(size).ToList();
      var count = list.Count();
      pag.Page = count / size + (count % size == 0 ? 0 : 1);
      return pag;
    }

    public class Pages
    {
      public List<类名> 类别名{ get; set; }
      public int Page { get; set; }
    }

     --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    后台方法存储过程

    [HttpGet]
            public PageDate GetGoods2(int index, int size)
            {
                var p = new DynamicParameters();
                p.Add("@index", index);
                p.Add("@size", size);
                p.Add("@totalcount", dbType: DbType.Int32, direction: ParameterDirection.Output);  //总数据数
                p.Add("@pagecount", dbType:DbType.Int32,direction:ParameterDirection.Output);      //总页数

                List<Goods> list = new List<Goods>();
                using (SqlConnection conn=new SqlConnection(connstr))
                {
                    list = conn.Query<Goods>("sp_Show",p,commandType:CommandType.StoredProcedure).ToList();
                }
                PageDate page = new PageDate();
                page.List = list;
                page.PageCount = p.Get<int>("@pagecount");
                return page;
            }

    create proc sp_Show
    (
    @index int,
    @size int,
    @totalcount int out,  --总数据数
    @pagecount int out  --总页数
    )
    as
    begin 
    --如果当前页数小于一
    if(@index<1)
    begin
    set @index=1
    end

    --计算总数据数
    select @totalcount=count(*) from Goods
    --计算总页数
    set @pagecount=CEILING(@totalcount*1.0/@size)

    --分页查询
    select * from 
    (select *,ROW_NUMBER() over (order by GId) rn from Goods) tb1 where rn between (@index-1)*@size+1 and @index*@size

    end

    declare @x int,@y int
    exec sp_Show 1,2,@x out,@y out
    select @x,@y

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    前台代码

    <div>
      <input id="Button1" onclick="first()" type="button" value="首页" />
      <input id="Button1" onclick="prev()" type="button" value="上一页" />
      <input id="Button1" onclick="next()" type="button" value="下一页" />
      <input id="Button1" onclick="last()" type="button" value="尾页" />
    </div>

    <script>
      var index1 = "";
      var pagecount = "";
      function first() {
        index1 = 1;
        log(index1);
      }
      function prev() {
        index1--;
        if (index1 == 0) {
          index1 = 1;
        }
        log(index1);
      }
      function next() {
        index1++;
        if (index1 > pagecount) {
          index1 = pagecount;
        }
        log(index1);
      }
      function last() {
        log(pagecount);
      }
    </script>

  • 相关阅读:
    转 | 禁忌搜索算法(Tabu Search)求解带时间窗的车辆路径规划问题详解(附Java代码)
    Branch and price and cut求解传统VRP问题以及VRPTW问题
    标号法(label-setting algorithm)求解带时间窗的最短路问题(ESPPRC)
    运筹学从何学起?如何快速入门精确式算法?
    转 | 模拟退火算法(SA)和迭代局部搜索(ILS)求解TSP的Java代码分享
    用Python画论文折线图、曲线图?几个代码模板轻松搞定!
    45. 截取“测试数据”后面的内容
    44. 更改oracle字符集编码american_america.zh16gbk 改为 SIMPLIFIED CHINESE_CHINA.ZHS16GBK
    18. 浏览器关闭页面时弹出“确定要离开此面吗?”
    6. concat_ws用法
  • 原文地址:https://www.cnblogs.com/Ai-Dou/p/13444136.html
Copyright © 2011-2022 走看看