对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.