对access数据库维护_Delphi

  • 内容
  • 评论
  • 相关

{***************************************************************
*
* Unit Name: unaccessTools
* Purpose  : Provide tools to compact and repair Access database.
*
****************************************************************}

unit unAccessTools;

interface

http://www.gaodaima.com/?p=65994对access数据库维护_Delphi

uses Sysutils,ComObj,Dialogs;

function DaoActive(var DaoObject:OleVariant):Boolean;
function DaoCompactDB(const FileName:string):Boolean;
function DaoRepairDB(const FileName:string):Boolean;

implementation

function DaoActive(var DaoObject:OleVariant):Boolean;
begin
  Result:=False;
  try
    DaoObject:=GetActiveOleObject('DAO.DBEngine.36');
    Result:=True;
  except
    try
      DaoObject:=CreateOleObject('DAO.DBEngine.36');
      Result:=True;
    except
      DaoObject:=Null;
    end;
  end;
end;

//压缩Access数据库
function DaoCompactDB(const FileName:string):Boolean;
var
  db:OleVariant;
  TempFile:string;
begin
  Result:=False;
  try
    if not DaoActive(db) then
      Exit;
    try
      TempFile:=ExtractFilePath(FileName)+'msaTemp.mdb';
      db.CompactDatabase(FileName,TempFile);
      DeleteFile(FileName);
      RenameFile(TempFile,FileName);
      Result:=True;
    except
      on E:EOleException do
        ShowMessage(E.Message);
    end
  finally
    db:=Unassigned;
  end;
end;

//修复Access数据库
function DaoRepairDB(const FileName:string):Boolean;
var
  db:OleVariant;
begin
  Result:=False;
  try
    if not DaoActive(db) then
      Exit;
    try
      db.RepairDatabase(FileName);
      Result:=True;
    except
      on E:EOleException do
        ShowMessage(E.Message);
    end
  finally
    db:=Unassigned;
  end;
end;

end.

欢迎大家阅读《对access数据库维护_Delphi》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码

原创文章,转载请注明: 转载自搞代码

本文链接地址: 对access数据库维护_Delphi

微信支付二维码

微信 赏一包辣条吧~

支付宝支付二维码

支付宝 赏一听可乐吧~

评论

0条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注