首页 | 社区 | 博客 | 招聘 | 文章 | 新闻 | 下载 | 读书 | 代码
亲,您未登录哦! 登录 | 注册

对access数据库维护

打印文章

分享到:
{***************************************************************
*
* Unit Name: unAccessTools
* Purpose  : Provide tools to compact and repair Access database.
*
****************************************************************}

unit unAccessTools;

interface

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.

本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )

编程爱好者论坛

本栏最新文章