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

          ACCESS中的分頁解決方案

          Posted on 2007-04-24 14:11 semovy 閱讀(448) 評論(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;
          }
          主站蜘蛛池模板: 恩平市| 永修县| 澄迈县| 特克斯县| 富源县| 思南县| 望谟县| 二手房| 岑溪市| 崇礼县| 万州区| 乐东| 西乌| 酒泉市| 探索| 永新县| 孟津县| 商水县| 楚雄市| 纳雍县| 兰溪市| 五常市| 宁强县| 英德市| 庆云县| 尼木县| 济宁市| 台南县| 贞丰县| 长宁区| SHOW| 鹤岗市| 茌平县| 瓦房店市| 德昌县| 湟源县| 乌拉特中旗| 云霄县| 达孜县| 彭水| 江城|