Delphi 压缩Access数据库

 

由于Access数据库在反复使用过程中会自动增大,Delphi压缩Access数据库的简单方法如下:
首先要引用ComObj单元

function TForm1.CompactMDB(const DBFile, Pwd: string): Boolean;
var
  TempDBFile:string;
  ConStr:string;
  JE:OleVariant;
begin
  ConStr := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;'
                                +'Jet OLEDB:Database Password=%s;';
  TempDBFile:=ExtractFilePath(Application.ExeName)+'Temp.mdb';
  try
    JE:=CreateOleObject('JRO.JetEngine');
    JE.CompactDatabase(Format(ConStr,[DBFile,Pwd]),
        Format(ConStr,[TempDBFile,Pwd]));
     Result:=CopyFile(PChar(TempDBFile),PChar(DBFile),False);
    DeleteFile(PChar(TempDBFile));
  except
    Result:=False;
  end;
end;

调用方法:

CompactMDB(ExtractFilePath(Application.ExeName)+'Test.mdb','');//没有密码,Pwd为空
CompactMDB(ExtractFilePath(Application.ExeName)+'Test.mdb','123456');//有密码

你可能感兴趣的:(Access)