下面的函数用来压缩access数据库 需要增加comobj单元
function compactdatabase(afilename,apassword:string):boolean;
//压缩与修复数据库,覆盖源文件
const
sconnectionstring = 'provider=microsoft.jet.oledb.4.0;data source=%s;'
+'jet oledb:database password=%s;';
var
spath,sfile:array [0..254] of char;
stempfilename:string;
je:olevariant;
begin
gettemppath(40,spath);//取得windows的temp路径
gettempfilename(spath,'~cp',0,sfile);//取得temp文件名,windows将自动建立0字节文件
stempfilename:=sfile;//pchar->string
deletefile(stempfilename);//删除windows建立的0字节文件
try
je:=createoleobject('jro.jetengine');//建立ole对象,函数结束ole对象超过作用域自动释放
olecheck(je.compactdatabase(format(sconnectionstring,[afilename,apassword]),
format(sconnectionstring,[stempfilename,apassword])));//压缩数据库
//复制并覆盖源数据库文件,如果复制失败则函数返回假,压缩成功但没有达到函数的功能
result:=copyfile(pchar(stempfilename),pchar(afilename),false);
deletefile(stempfilename);//删除临时文件
except
result:=false;//压缩失败
end;
end;