Analysis on the Method of Constructing Paging Application with C
- 2021-12-04 10:56:15
- OfStack
In this paper, an example is given to describe the method of building paging application in C #. Share it for your reference, as follows:
1. SQL statement
WITH [temptableforStockIC] AS (
SELECT *,ROW_NUMBER() OVER (ORDER BY CreateTime DESC) AS RowNumber FROM [StockIC] WHERE 1=1 AND Model = 'FTY765OP'
)
SELECT * FROM [temptableforStockIC] WHERE RowNumber BETWEEN 1 AND 10
2. Background 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.OrderBy))
{
param.OrderBy = " 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.OrderBy, 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.
1 Some calculation methods
// Paging
Int64 startNumber = (param.PageIndex - 1) * param.PageSize + 1;
Int64 endNumber = startNumber + param.PageSize - 1;
// Total pages = ( Total data + Page size -1) / Page size
TotalPage = (TotalCount + PageSize - 1) / PageSize;
For more readers interested in C # related contents, please check the topics on this site: "Summary of C # String Operation Skills", "Summary of C # Array Operation Skills", "Summary of XML File Operation Skills in C #", "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Data Structure and Algorithm Tutorial" and "C # Object-Oriented Programming Introduction Tutorial"
I hope this article is helpful to everyone's C # programming.