Batman
جمعه 20 آذر 1388, 08:24 صبح
با سلام
تو سایت چند تا نمونه کد برای این کار بود:
function DatabaseCompact(const sdbName: WideString): boolean;
// This function compacts an Access 2000 database
// it is called from Backup window
var
JE : TJetEngine; //Jet Engine
sdbTemp : WideString; //TEMP database
sdbTempConn : WideString; //Connection string
const
SProvider = 'Provider=Microsoft.jet.OLEDB.4.0;'+
'Data Source=';
begin
Result:=False;
//Here we make temp file's name
//JE.CompactDatabase() does not let you to have same names...
//...as source and destination parameter, so we neet a temp file
sdbTemp := ExtractFileDir(sdbName) +
'TEMP' +
ExtractFileName(sdbName);
sdbTempConn := SProvider + sdbtemp;
//if destination file is exists, JE.compactDatabase raises an exception
//so we need to check if the file exists, if it does we have to delete it
if FileExists(sdbTemp) then
DeleteFile(sdbTemp);
//Here we make an instance of JetEngine
JE:= TJetEngine.Create(Application);
try
try
Application.ProcessMessages;
//Here is the command which compacts our database
JE.CompactDatabase(SProvider + sdbName, sdbTempConn);
//Here we replace compact database(Temp database) with current one
DeleteFile(sdbName);
RenameFile(sdbTemp, sdbName);
except
MessageDlg('خطایی در هنگام فشرده سازی بانک اطلاعاتی رخ داده است',mtError,[mbOK],0);
end;
finally
//At last here we set JetEngine instance free
JE.FreeOnRelease;
Result :=True;
end;
end;
Function CompactAndRepair(DB: string) : boolean;
var
v: OLEvariant;
begin
Result := True;
try
v := CreateOLEObject('JRO.JetEngine');
try
v.CompactDatabase('Provider=Microsoft.jet.OLEDB.4. 0;Data Source=' + DB,
'Provider=Microsoft.jet.OLEDB.4.0;Data Source=' + DB +
'x;Jet OLEDB:Engine type=5');
DeleteFile(DB);
RenameFile(DB + 'x', DB)
finally
v := Unassigned
end;
except
Result := False
end;
end;
اما هر دو رو تست کردم.هیچکدوم با اکسس 2007 کار نمیکنه.
لطفا راهنماییم کنید.
ممنونم
تو سایت چند تا نمونه کد برای این کار بود:
function DatabaseCompact(const sdbName: WideString): boolean;
// This function compacts an Access 2000 database
// it is called from Backup window
var
JE : TJetEngine; //Jet Engine
sdbTemp : WideString; //TEMP database
sdbTempConn : WideString; //Connection string
const
SProvider = 'Provider=Microsoft.jet.OLEDB.4.0;'+
'Data Source=';
begin
Result:=False;
//Here we make temp file's name
//JE.CompactDatabase() does not let you to have same names...
//...as source and destination parameter, so we neet a temp file
sdbTemp := ExtractFileDir(sdbName) +
'TEMP' +
ExtractFileName(sdbName);
sdbTempConn := SProvider + sdbtemp;
//if destination file is exists, JE.compactDatabase raises an exception
//so we need to check if the file exists, if it does we have to delete it
if FileExists(sdbTemp) then
DeleteFile(sdbTemp);
//Here we make an instance of JetEngine
JE:= TJetEngine.Create(Application);
try
try
Application.ProcessMessages;
//Here is the command which compacts our database
JE.CompactDatabase(SProvider + sdbName, sdbTempConn);
//Here we replace compact database(Temp database) with current one
DeleteFile(sdbName);
RenameFile(sdbTemp, sdbName);
except
MessageDlg('خطایی در هنگام فشرده سازی بانک اطلاعاتی رخ داده است',mtError,[mbOK],0);
end;
finally
//At last here we set JetEngine instance free
JE.FreeOnRelease;
Result :=True;
end;
end;
Function CompactAndRepair(DB: string) : boolean;
var
v: OLEvariant;
begin
Result := True;
try
v := CreateOLEObject('JRO.JetEngine');
try
v.CompactDatabase('Provider=Microsoft.jet.OLEDB.4. 0;Data Source=' + DB,
'Provider=Microsoft.jet.OLEDB.4.0;Data Source=' + DB +
'x;Jet OLEDB:Engine type=5');
DeleteFile(DB);
RenameFile(DB + 'x', DB)
finally
v := Unassigned
end;
except
Result := False
end;
end;
اما هر دو رو تست کردم.هیچکدوم با اکسس 2007 کار نمیکنه.
لطفا راهنماییم کنید.
ممنونم