Access的数据库必须关闭才能压缩,这个代码可以:
设此数据库的密码是 idlehagar
use JRO_TLB;
procedure CompactMdb;
const sProvider = 'Provider=Microsoft.Jet.OLEDB.4.0;';
var oJetEng: JetEngine;
sOldMdb, sNewMdb: string;
begin
sOldMdb := sProvider + 'Data Source=' + 'C:/Database/Test.mdb;'
+ 'Jet OLEDB
atabase Password=idlehagar';
sNewMdb := sProvider + 'Data Source=' + 'C:/Test.mdb;'
+ 'Jet OLEDB
atabase Password=idlehagar';
try
try
oJetEng := CoJetEngine.Create;
oJetEng.CompactDatabase(sOldMdb, sNewMdb);
if FileExists('C:/Test.mdb') then begin
CopyFile('C:/Test.mdb', 'C:/Database/Test.mdb', False);
DeleteFile('C:/Test.mdb');
end;
except
on E: Exception do
showmessage('Compact database failed');
end
finally
oJetEng := nil;
end;
end;