用程序实现压缩access(*.mdb)数据库的方法.

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 looyo 的 blog

下面的函数用来压缩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;

本文关键:用程序实现压缩access(*.mdb)数据库的方法.
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top