This article mainly introduces the method of building paging application in C #, and analyzes the concrete steps and related implementation techniques of C # to create paging function with instance form, and the friends who need can refer to the following
The example in this article describes how C # constructs a paging application. Share to everyone for your reference, as follows:
1. SQL statements
With [Temptableforstockic] as ( SELECT *,row_number () over (ORDER by Createtime DESC) as RowNumber from [Stockic] wher E 1=1 and Model = ' fty765op ') SELECT * from [Temptableforstockic] WHERE RowNumber between 1 and 10
2. Backstage method
<summary>///table name///</summary>private const string _tablenane = "Stockic";///<summary>///Get inventory list// /</summary>public list<stockicresult> getstockiclist (stockicparam param) {list<stockicresult> List = new list<stockicresult> (); String sql = "with [temptablefor{0}] as"; SQL + = "(select *,row_number () over (ORDER by {1}) as RowNumber from [{0}] WHERE 1=1 {2})"; SQL + = "SELECT * FROM [temptablefor{0}] WHERE RowNumber between {3} and {4}"; StringBuilder sqlcondition = new StringBuilder (); list<sqlparameter> sqlparams = new list<sqlparameter> (); Model if (! String.IsNullOrEmpty (param. Model) {Sqlcondition.appendformat ("and model like '%{0}% '", Param. Model); }//Start time if (param. Begintime.hasvalue) {sqlcondition.append ("and Createtime >= @BeginTime"); Sqlparams.add (New SqlParameter ("@BeginTime", Param. Begintime.value)); }//End time if (param. Endtime.hasvalue) {sqlcondition.append ("and Createtime < @ENdtime "); Sqlparams.add (New SqlParameter ("@EndTime", Param. EndTime.Value.AddDays (1))); }//Sort if (String.isnullorwhitespace (param). ) {param. = "Createtime DESC"; }//Paging param. PageIndex = param. PageIndex-1; Int64 startnumber = param. PageIndex * param. PageSize + 1; Int64 endnumber = Startnumber + param. PageSize-1; Assemble SQL sql = String.Format (sql, _tablenane, param. Sqlcondition, Startnumber, Endnumber); Execute SQL statement DataSet DataSet = Dbhelper.getreader (SQL. ToString (), Sqlparams.toarray ()); List = Trantolist (DataSet); return list;}
Note: Dbhelper.getreader () method, Trantolist () method, etc. please improve yourself.
Some calculation methods
Paging Int64 Startnumber = (param. PageIndex-1) * param. PageSize + 1;int64 endnumber = Startnumber + param. pagesize-1;//Total Pages = (total data + paging size-1)/Paging size Totalpage = (totalcount + PageSize-1)/PageSize;