下面agritax.mdb是目标数据库,数据库的口令是agriculture,如果没有口令则还可以简单点
我用的是Access97需引用dao97
function daocompactdb:boolean
//compactdatabase
var
srcfile,dstfile:widestring;
db:_DBEngine;
dtbs:database;
begin
srcfile:=extractfilepath(application.ExeName)+'agritax.mdb';
dstfile:=extractfilepath(application.ExeName)+'tempfile.mdb';
try
try
db:=coDBEngine.Create;
dtbs:=db.OpenDatabase(srcfile,true,false,';pwd=agriculture');
dtbs.NewPassword('agriculture','');
dtbs.Close;
dtmd.DataModule1.adocntion.Connected:=false;
db.CompactDatabase(srcfile,dstfile,emptyparam,emptyparam,'');
dtmd.DataModule1.adocntion.Connected:=false;
if not copyfile(pchar(dstfile),pchar(srcfile),false) then
begin
deletefile(dstfile);
result:=false;
exit;
end;
finally
dtbs:=db.OpenDatabase(srcfile,true,false,';pwd=');
dtbs.NewPassword('','agriculture');
dtbs.Close;
dtmd.DataModule1.adocntion.Connected:=true;
end;
result:=true;
except // wrap up
dtmd.DataModule1.adocntion.Connected:=true;
result:=false;
dtmd.DataModule1.adocntion.Connected:=true;
end
// try/except
deletefile(dstfile);
dtmd.DataModule1.adocntion.Connected:=true;
end;
function daorepairdb:boolean
//repairdatabase
var
db:_DBEngine;
begin
db:=coDBEngine.Create;
try
db.RepairDatabase(extractfilepath(application.ExeName)+'agritax.mdb');
result:=true;
except
result:=false;
end;
end;