Home  >  Article  >  Database  >  Access 数据表分页显示&导入数据库

Access 数据表分页显示&导入数据库

WBOY
WBOYOriginal
2016-06-07 15:32:301153browse

以 Delphi 开发基于 Oracle/SQL Server 的数据库应用程序,在显示表中数据时,若数据量过大,难免要应用 分页 技术――幸运的是,诸于此两者的大型 DBMS,对 分页 提供了良好的支持,但对 M$Access 来说却要麻烦些――前提自然是 Access 表中没有自增字段―

 以 Delphi 开发基于 Oracle/SQL Server 的数据库应用程序,在显示表中数据时,若数据量过大,难免要应用 分页 技术――幸运的是,诸于此两者的大型 DBMS,对 分页 提供了良好的支持,但对 M$  Access 来说却要麻烦些――前提自然是 Access 表中没有自增字段――而这正是关键...

    实现步骤:
    一、变量定义
    FTotalRecCountMDB: Integer; //Access 库中表的记录数
    FFieldLstAndType: string; //字段及字段类型列表
    FFieldLst: string; //字段列表
  
    二、变量使用(示例)
    FFieldLstAndType:= 'Name varchar(255); Age integer; Addr varchar(255)';
    FFieldLst:= 'Name; Age; Addr';

    三、准备
    //创建同结构临时表――多出 ID 自增字段!
    //aqy_Tmp 已连接至对应的 Access
    with aqy_Tmp do //aqy_Tmp: TADOQuery
    begin
      Close;
      SQL.Text:= 'Create Table _TabTmp(ID Counter PRIMARY KEY,' + FFieldLstAndType + ')';
      ExecSQL;
      Close;
    end;

    //将所选的表中的数据 Copy 到临时表
    with aqy_Tmp do
    begin
      Close;

      SQL.Text:=
        'Insert Into _TabTmp(' + FFieldLst + ')' +
        'Select ' + FFieldLst + ' From ' + edt_TabName.Text; //edt_TabName: TEdit;

      ExecSQL;
      Close;
    end;

    四、OK,分页显示&导入DB //每 万条 分一页,    var
      i{, j}: integer;      
      BatchCount: Integer;
    begin
      with aqy_Tmp do
      begin
        if FTotalRecCountMDB         begin
          if Active then
            Close;

          SQL.Text:= 'select * from _TabTmp';
          Open;
          
          //then import datas into DB
          {while not Eof do          
           ...
           Next;
          }
        end
        else
        begin
          if FTotalRecCountMDB mod 10000 = 0 then
            BatchCount:= FTotalRecCountMDB div 10000
          else
            BatchCount:= FTotalRecCountMDB div 10000 + 1;

          for i:= 0 to BatchCount - 1 do
          begin
            if Active then
              Close;

            //分页
            SQL.Text:=
              'select * from _TabTmp where ID Between ' +
              IntToStr((i * 10000) + 1) + ' and ' + IntToStr((i + 1) * 10000);
              
            Open;
            
            //other codes that import datas to DB
            {while not Eof do
             ...
             Next;
            }
          end; //end for loop    
        end; //batch end

        //delete _TabTmp
        if Active then
          Close;
        SQL.Text:= 'Drop Table _TabTmp';
        ExecSQL;
        Close;
      end; //end with aqy_Tmp
    end;
    
    OK,Done!

    实际应用中当然至少还应加进 异常处理,及 Application.ProcessMessages 以免在导数据入库过程中程序假死~

ADelphiCoder

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn