C# data paging

background

        In fact, the paging function has been around for a long time, but every time it is written down, I don’t know where it is lost. Because it is not in the same project, I am too lazy to look through it one by one, and the data type given in each query is still unclear. It's not the same, but you can write a method to transfer it. Of course, I didn't write it, hehe!

analyze

        In fact, when it comes to data paging, it can be divided into three types : front-end, back-end, and database . This is what I think. The following content is all what I may have seen somewhere before and some of my own understanding later. Just look at it like this, maybe There are other theories as well.

  • For the front-end, I also used native html and the methods provided by the EasyUI framework , but now, hey, I have been writing interfaces and I don’t know how to do it anymore. I can search on Baidu and it should be OK.
  • In terms of backend, we can actually divide it into true fractionation (actually database paging) and false fractionation . False fractionation is to find out the data in the database, and then display the data according to the incoming page page and number of rows; for backend paging, if It is a List collection (the previous article seems to have covered it, and if there is no data, at least no error will be reported, and there is no need to use Sanmu to process it) , just use Linq syntax directly. If it is a DataTable , you can use AsEnumerable() or Convert DataTable to List and then split it
  • Database paging only queries the required data (actually not, but it is the most true thing I understand so far, because it also involves SQL optimization issues and is also related to the amount of data)

auxiliary tool

  • dll related
    • System.Linq (back-end paging related, splitting data)
    • System.Collections.Generic (It seems AsEnumerable() is useful, not sure)
  • development tools
    • Backend Development VS 2019
    • DatabaseDbeaver

backend code


DataTable dt = new DataTable();
var Data = dt.Rows.Count > 0 ? dt.AsEnumerable().Skip(--var_page * var_rows).Take(var_rows).CopyToDataTable() : dt;
var Count = dt.Rows.Count > 0 ? dt.AsEnumerable().Count() : dt.Rows.Count;

Note: The ternary operator is used here? :, because if the dt data is empty here, an error will be reported when splitting the data. The source contains no DataRows.

DatabaseSQL

-- 取数据11-20,也就是rows=10;page=2;但要注意你的数据page是从0开始取数据,还是1开始取数据
 SELECT *
  FROM (SELECT *
          FROM (SELECT ROW_NUMBER () OVER (ORDER BY t.columns) AS rownumber, t.*
                  FROM (Table) t) p
         WHERE p.rownumber <= 20) table_alias
 WHERE rownumber > 10;


 SELECT *
  FROM (SELECT *
          FROM (SELECT ROW_NUMBER () OVER (ORDER BY t.columns) AS rownumber, t.*
                  FROM (Table) t) p
         WHERE p.rownumber > 0) table_alias
 WHERE rownumber <= 10;

Notice:

  • Fetch data 11-20, that is, rows=10;page=2; but pay attention to whether your data page starts fetching data from 0 or 1 .
  • The rownumber greater than sign can also be used in reverse. I saw this on someone else's blog before, but I didn't record whose blog it was.
--我的新宠,最好用有索引排序的栏位来排序,速度会快很多, 
 --单ROWNUM <= 6000000的上限大概是六百万,大于这个这个数,查询时间超过一秒
 --ROWNUM <= 6000000且table_alias.rowno > 599990,这两个数值越接近,查询速度越慢,两个条件同时满足,大概适用ROWNUM <= 600000(60万的数据),这样分页显示一页十行不超过一秒
 
  SELECT *
  FROM (SELECT tt.*, ROWNUM AS rowno
          FROM (  SELECT t.*
                    FROM Table t  
                ORDER BY CREATEDT DESC) tt
         WHERE ROWNUM <= 600000 --and CREATEDT between to_char(sysdate-30,'yyyy-MM-dd') and to_char(sysdate,'yyyy-MM-dd') 
         ) table_alias
 WHERE table_alias.rowno > 599990  
 ; 

Well, these SQLs were all written by Baidu when there was demand at that time. Now they are just sorted out, so for verification, I have not done them. You can refer to them and I will record them.

Reference article

c# DataTable data is divided into multiple and paginated_51CTO blog_c# datatable merge

Guess you like

Origin blog.csdn.net/qq_41128526/article/details/127651031