DataGrid connection ACCESS fast paging method (4) - Dynamically generate SQL statements
Using system;
Using system.text;
Namespace Paging
{
///
/// FastPaging summary description.
/// summary>
PUBLIC CLASTPAGING {
Private fastpaging () {
}
///
/// Get the SELECT statement that is sorted and paid according to the specified field.
/// summary>
/// The number of records to display per page. param>
/// The index of the page to display. param>
/// The total number of records in the data table. param>
/// The data table to query. param>
/// The field you want to query. param>
/// Primary key field. param>
/// is arranged as ascending. param>
/// Filtering criteria for query. param>
///
Public static string paning Page
Int PageSize,
Int pageIndex,
INT Recordcount,
String Tablename,
String queryfields,
String PrimaryKey,
Bool ascending,
String condition
{
#Region implementation
StringBuilder SB = New StringBuilder ();
INT PageCount = getPageCount (RecordCount, Pagesize); // Package total
INT MIDDLEINDEX = getMidpageIndex (pageCount); // Index of the intermediate page
INT firstIndex = 0; // The index of the first page
INT LastIndex = PageCount - 1; // The index of the last page
#Region @PageIndex <= @firstindex
IF (PageIndex <= firstIndex) {
SB.Append ("SELECT TOP") .append ("") .append (queryfields)
.Append ("from") .append (tablename);
IF (Condition! = String.empty)
Sb.append ("where") .append (condition); sb.append ("order by") .append (primarykey) .append (")
.Append (getSortType (Ascending));
}
#ndregion
#Region @firstindex <@PageIndex <= @middleindex
Else IF (PageIndex> FirstIndex && pageindex <= middleindex) {
SB.Append ("SELECT TOP") .append ("") .append (queryfields)
.Append ("from") .append (TableName)
.Append ("where") .append (primarykey);
IF (ascending)
Sb.append (">) .append (" SELECT MAX (");
Else
sb.append ("<(") .append ("SELECT MIN (");
Sb.append (primarykey) .append (") from (select top")
.Append ("") .append ("") .append (primarykey)
.Append ("from") .append (tablename);
IF (Condition! = String.empty)
Sb.append ("where") .append (condition);
Sb.append ("ORDER BY") .append (primarykey) .append ("")
.Append (getSortType (Ascending))
.Append (") Tablea);
IF (Condition! = String.empty)
sb.append ("and") .append (condition);
Sb.append ("ORDER BY") .append (primarykey) .append ("")
.Append (getSortType (Ascending));
}
#ndregion
#Region @MiddleIndex <@PageIndex <@LastIndex
Else IF (PageIndex> MiddleIndex && PageIndex Sb.append ("SELECT *" .Append (PageSize) .append ("") .append (queryfields) .Append ("from") .append (TableName) .Append ("where") .append (primarykey); IF (ascending) sb.append ("<(") .append ("SELECT MIN ("); Else Sb.append (">) .append (" SELECT MAX ("); Sb.append (primarykey) .append (") from (select top") .append (RecordCount-PageSize *). Append ("") .append (primarykey) .Append ("from") .append (tablename); IF (Condition! = String.empty) Sb.append ("where") .append (condition); Sb.append ("ORDER BY") .append (primarykey) .append ("") .Append (GetSortType (! Ascending)) .Append (") Tablea); IF (Condition! = String.empty) sb.append ("and") .append (condition); Sb.append ("ORDER BY") .append (primarykey) .append ("") .Append (GetSortType (! Ascending)) .Append (") Tableb Order By") .append (primarykey) .append ("") .Append (getSortType (Ascending)); } #ndregion #Region @PageIndex> = @LastIndex Else IF (pageindex> = lastindex) { Sb.append ("SELECT * from (select top") .append (recordcount-pagesize * lastindex) .Append ("") .append (queryfields) .Append ("from") .append (tablename); IF (Condition! = String.empty) Sb.append ("where") .append (condition); Sb.append ("ORDER BY") .append (primarykey) .append ("") .Append (GetSortType (! Ascending)) .Append (") Tablea Order By") .append (primarykey) .append ("") .Append (getSortType (Ascending)); } #ndregion Return sb.toString (); #ndregion } /// /// Get the SELECT statement that is sorted and paid according to the specified field. /// summary> /// The number of records to display per page. param> /// The index of the page to display. param> /// The total number of records in the data table. param> /// The data table to query. param> /// The field you want to query. param> /// Primary key field. param> Public static string paning Page Int PageSize, Int pageIndex, INT Recordcount, String Tablename, String queryfields, String primarykey) { Return Paging (PageSize, PageIndex, Recordcount, TableName, Queryfields, PrimaryKey, TRUE, STRING.EMPTY; } /// /// Get the SELECT statement that is sorted and paid according to the specified field. /// summary> /// The number of records to display per page. param> /// The index of the page to display. param> /// The total number of records in the data table. param> /// The data table to query. param> /// The field you want to query. param> /// Primary key field. param> /// is arranged as ascending. param> /// Public static string paning Page Int PageSize, Int pageIndex, INT Recordcount, String Tablename, String queryfields, String PrimaryKey, Bool ascending) { Return Paging (PageSize, PageIndex, Recordcount, TableName, Queryfields, PrimaryKey, ascending, string.empty; } /// /// Get the SELECT statement that is sorted and paid according to the specified field. /// summary> /// The number of records to display per page. param> /// The index of the page to display. param> /// The total number of records in the data table. param> /// The data table to query. param> /// The field you want to query. param> /// Primary key field. parame> /// Filtering criteria for query. param> /// Public static string paning Page Int PageSize, Int pageIndex, INT Recordcount, String Tablename, String queryfields, String PrimaryKey, String condition { Return Paging (PageSize, PageIndex, Recordcount, TableName, Queryfields, PrimaryKey, TRUE, CONDITION); } /// /// Calculate the number of pages. /// summary> /// The total number of records is recorded. param> /// The number of records shown per page. param> /// Public Static Int getPageCount (int recordcount, int pageize) { Return (int) Math.ceiling (Double) Recordcount / Pagesize; } /// /// Calculate the page index of the intermediate page. /// summary> /// Sub-page number. param> /// Public Static Int getMidPageIndex (int PageCount) { Return (int) Math.ceiling (Double) PageCount / 2) - 1; } /// /// Get sorted mode ("ASC" means an ascending order, "DESC" means descending order). /// summary> /// is ascending. param> /// Public Static String GetSortType (Bool Ascending) { RETURN (Ascending? "ASC": "DESC"); } /// /// Get a Boolean value, which indicates whether the way is sorted is ascending. /// summary> /// Sorted mode ("ASC" represents ascending, "DESC" means descending.). param> /// { Return (Ordertype.toupper () == "DESC")? false: true); } } } Author: Tripoli