请问如何压缩加了密码的ACCESS数据库??不使用ODBC而用连接字符串,没有加密码的ACCESS数据库压缩成功而加了密码就失败,为什么?如何做??高手指教!!!

解决方案 »

  1.   

    Function CompactAndRepair(sOldMDB:String;sNewMDB:String;psw:string;password:boolean):Boolean;
    const
      sProvider='Provider=Microsoft.Jet.OLEDB.4.0;';
    var
      oJetEng:JetEngine;
    begin
      if password then
        sOldMDB:=sProvider+'Data Source='+sOldMDB+';jet oledb:database password="'+psw+'"'
      else
        sOldMDB:=sProvider+'Data Source='+sOldMDB;
        
      if password then
         sNewMDB:=sProvider+'Data Source='+sNewMDB+';jet oledb:database password="'+psw+'"'
      else
         sNewMDB:=sProvider+'Data Source='+sNewMDB;
      try
        oJetEng:=CoJetEngine.Create;
        oJetEng.CompactDatabase(sOldMDB, sNewMDB);    
        oJetEng:=Nil;
        Result:=True;
      except
        oJetEng:=Nil;
        Result:=False;
      end;
    end; {10.CompactAndRepair(压缩Access数据库,用到JRO_TLB单元,可按下面方法加入)   }
    { a) 在Delphi IDE中选择Project - Import Type Library.                      }
    { b) 往下翻直到你找到“Microsoft Jet and Replication Objects 2.1 Library”.}
      

  2.   

    应该是你的连接字符串没写好的原因,压缩方法和不加密码的一样function CompactRepairMDB(const MDBName: string; const OldPWD: string = '';
      const NewPWD: string = ''; const MDBType: string = '97'): Boolean;
    //压缩修复MDB数据库,也可修改数据库口令;
    //MDBName数据库文件名,OLDPWD旧口令,NEWPWD新口令。
    var
      MyJetEngine: JetEngine;
      sTempFile, sOldJRO, sNewJRO: string;
    begin
      Result := False;
      //Check file exist.
      if not FileExists(MDBName) then
      begin
        raise Exception.Create('MS Access数据库:' + #13 + MDBName + #13
          + '不存在!');
        Exit;
      end;
      //Set environment
      sTempFile := GetTempFile;
      sOldJRO := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + MDBName;
      sNewJRO := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + sTempFile;
      if MDBType = '97' then
      begin
        sOldJRO := sOldJRO + ';Jet OLEDB:Engine Type=4';
        sNewJRO := sNewJRO + ';Jet OLEDB:Engine Type=4';
      end
      else if MDBType = '2000' then begin
        sOldJRO := sOldJRO + ';Jet OLEDB:Engine Type=5';
        sNewJRO := sNewJRO + ';Jet OLEDB:Engine Type=5';
      end
      else begin
        raise Exception.Create('本函数不支持建立其他格式的MS Access数据库!');
        Exit;
      end;
      if OldPWD <> '' then
        sOldJRO := sOldJRO + ';Jet OLEDB:Database Password=' + OldPWD;
      if NewPWD <> '' then
        sNewJRO := sNewJRO + ';Jet OLEDB:Database Password=' + NewPWD;
      //Compact
      try
        try
          MyJetEngine := CoJetEngine.Create();
          MyJetEngine.CompactDatabase(sOldJRO, sNewJRO);
          Result := True;
        except
          raise Exception.Create('无法修复和压缩MS Access ' + MDBType + ' 数据库:'
            + #13 + MDBName);
        end;
      finally
        MyJetEngine := nil;
      end;
      //Rename file.
      if not Result then
        Exit;
      //Copy File
      if not CopyFile(PChar(sTempFile), PChar(MDBName), False) then
      begin
        raise Exception.Create('无法复制文件!');
        Result := False;
        Exit;
      end;
      //Delete temp file.
      if not DeleteFile(PChar(sTempFile)) then
      begin
        raise Exception.Create('无法删除临时文件!');
        Result := False;
        Exit;
      end;  Result := True;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
    if CompactRepairMDB(gs_dbname,'Roadster','Roadster','2000') = false then
     Application.MessageBox('压缩和修复过程出现错误!!','数据库工具',Mb_Ok+Mb_IconError)
    else
     Application.MessageBox('恭喜,已成功压缩和修复!','数据库工具',Mb_Ok+Mb_IconInformation);
    end;