用程序实现压缩access(*.mdb)数据库的方法
下面的函数用来压缩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 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,),
format(SConnectionString,)));//压缩数据库
//复制并覆盖源数据库文件,如果复制失败则函数返回假,压缩成功但没有达到函数的功能
result:=CopyFile(PChar(STempFileName),PChar(AFileName),false);
DeleteFile(STempFileName);//删除临时文件
except
result:=false;//压缩失败
end;
end;
[ 本帖最后由 Moodsky 于 2007-2-8 10:15 编辑 ] uses dao97,dao2000;
procedure CompactMdbDatabase(srcDbname,dstDbname,oldpwd,newpwd:string;bAccess97:boolean=true);
var idbEngine:_dbEngine;
begin
if oldpwd <>'' then oldpwd := ';pwd='+oldpwd;
if newpwd <>'' then newpwd := ';pwd='+newpwd;
if bAccess97 then
begin
idbengine := CreateComObject(dao97.CLASS_DBEngine) as _DBEngine;
idbEngine.CompactDatabase(srcDbname,dstDbname,newpwd,dbVersion30,oldpwd);
end else
begin
idbengine := CreateComObject(dao2000.CLASS_DBEngine) as _DBEngine;
idbEngine.CompactDatabase(srcDbname,dstDbname,newpwd,dbVersion40,oldpwd);
end;
end;
页:
[1]