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

          日歷

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          隨筆分類

          隨筆檔案(1)

          收藏夾(18)

          搜索

          •  

          最新評論

          ACCESS中的分頁解決方案

          Posted on 2007-04-24 14:11 semovy 閱讀(447) 評論(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;
          }
          主站蜘蛛池模板: 五家渠市| 无极县| 南昌市| 称多县| 凤城市| 阜南县| 温宿县| 南木林县| 云梦县| 洛浦县| 连江县| 萨迦县| 丰县| 清新县| 庆城县| 牙克石市| 成武县| 达日县| 镇沅| 西和县| 永登县| 八宿县| 留坝县| 红原县| 沁阳市| 乾安县| 基隆市| 连州市| 绍兴市| 滁州市| 准格尔旗| 资溪县| 含山县| 日土县| 新龙县| 文安县| 义乌市| 广州市| 镇原县| 嵊州市| 马山县|