posts - 0, comments - 77, trackbacks - 0, articles - 356
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          ACCESS中的分頁解決方案

          Posted on 2007-04-24 14:11 semovy 閱讀(445) 評論(0)  編輯  收藏 所屬分類: C++ Builder相關
          String TUtilForm::getSQL(int recordCount ,int pageCount, int pageSize,int pageIndex,String tableName,String queryFields,String primaryKey,String ascending,String condition)
          {
              int middleIndex = pageCount/2;
              int firstIndex = 0;
              int lastIndex = pageCount - 1;
              String SQLStr = "";
              if( pageIndex <= firstIndex )
              {
                  SQLStr = "select top " + IntToStr(pageSize) + " " + queryFields + " from " + tableName;
                  if( condition != "")
                  SQLStr += " where " + condition ;
                  SQLStr += " order by " + primaryKey + " " + ascending;
              }
              else if( pageIndex > firstIndex && pageIndex <= middleIndex )
              {
                  SQLStr = "select top " + IntToStr(pageSize) + " " + queryFields + " from " + tableName + " where " + primaryKey ;
                  if( ascending == "asc" )
                  SQLStr +=  ">(select max(";
                  else
                  SQLStr += "<(select min(";
                  SQLStr += primaryKey + ") from (select top " + IntToStr(pageSize*pageIndex) + " " + primaryKey + " from " + tableName;
                  if( condition != "" )
                  SQLStr += " where " + condition;
                  SQLStr += " order by " + primaryKey + " " + ascending + ") TableA )";
                  if( condition != "" )
                  SQLStr += " and " + condition;
                  SQLStr += " order by " + primaryKey + " " + ascending;
              }
              else if( pageIndex > middleIndex && pageIndex < lastIndex)
              {
                  SQLStr = "select " + queryFields + " from (select top " + IntToStr(pageSize) + " " + queryFields + " from " + tableName;
                  SQLStr += " where " + primaryKey ;
                  if( ascending == "asc" )
                  SQLStr += " < ( select min(";
                  else
                  SQLStr += " > ( select max(";
                  SQLStr += primaryKey + ") from (select top " +  IntToStr( recordCount - pageSize * (pageIndex + 1)) + " " + primaryKey + " from " + tableName;
                  if( condition != "" )
                  SQLStr += " where " + condition;
                  SQLStr += " order by " + primaryKey + " desc) TableA)" ;
                  if( condition != "" )
                  SQLStr += " and " + condition;
                  SQLStr += " order by " + primaryKey + " desc) TableB order by )" + primaryKey + " " + ascending ;  
              }
              else
              {
                  SQLStr = "select " + queryFields + " from (select top " + IntToStr(recordCount - pageSize*lastIndex) + " " + queryFields + " from " + tableName;
                  if( condition != "" )
                  SQLStr += " where " + condition;
                  SQLStr += " order by " + primaryKey + " desc " + " )TableA order by " + primaryKey + " " + ascending;
              }
              return SQLStr;
          }
          主站蜘蛛池模板: 古蔺县| 法库县| 尚义县| 门源| 德安县| 张家口市| 华宁县| 富顺县| 婺源县| 蒙阴县| 太仓市| 洮南市| 大荔县| 东源县| 辽宁省| 辽源市| 金堂县| 北票市| 临桂县| 长岭县| 鹿邑县| 左贡县| 临江市| 邹城市| 启东市| 济源市| 大埔区| 宁河县| 枣阳市| 北碚区| 潞城市| 肥乡县| 铜山县| 涡阳县| 旌德县| 如东县| 承德市| 雷山县| 四会市| 舞阳县| 集贤县|