一段分页的代码
来源:互联网 发布:八王之乱 知乎 编辑:程序博客网 时间:2024/05/01 12:47
- public override System.Data.DataTable List(string className, string option, string orderField, string orderDirection, string primaryKey, int pageIndex, int pageSize, out int recordCount, out int pageCount)
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- string tableName = "tb" + className;
- string sql = "Select Count(" + primaryKey + ") As rcount From " + tableName;
- if (option != "")
- {
- sql += " Where " + option;
- }
- int resultCount = int.Parse(ExecuteScalar(sql, null).ToString());
- recordCount = resultCount;
- if (resultCount >= 0)
- {
-
- if (resultCount % pageSize == 0)
- {
- pageCount = resultCount / pageSize;
- }
- else
- {
- pageCount = (int)((resultCount / pageSize) + 1);
- }
-
- if (orderField != "")
- {
- orderField = " Order By " + orderField;
- if (orderDirection != "")
- {
- orderField += " " + orderDirection;
- }
- }
-
- if (pageIndex == 1)
- {
-
- if (option != "")
- {
- sql = "Select Top " + pageSize.ToString() + " * From " + tableName + " Where " + option + " " + orderField;
- }
- else
- {
- sql = "Select Top " + pageSize.ToString() + " * From " + tableName + " " + orderField;
- }
- }
- else
- {
- int minRecord = (pageIndex - 1) * pageSize;
-
- if (option != "")
- {
- sql = "Select Top " + pageSize.ToString() + " * From " + tableName + " Where " + primaryKey + " Not In(Select " + primaryKey + " From (Select Top " + minRecord + " " + primaryKey + " From " + tableName + " Where " + option + " " + orderField + ")TemA) And " + option + " " + orderField;
- }
- else
- {
- sql = "Select Top " + pageSize.ToString() + " * From " + tableName + " Where " + primaryKey + " Not In(Select " + primaryKey + " From (Select Top " + minRecord + " " + primaryKey + " From " + tableName + " " + orderField + " )temA) " + orderField;
- }
- }
- }
- else
- {
- pageCount = 0;
- }
- DataTable dt = ExecuteDataSet(sql, null).Tables[0];
- return dt;
- }