请看我以下备份,恢复,压缩代码错在那里?(0分)

  • 主题发起人 主题发起人 pokar
  • 开始时间 开始时间
P

pokar

Unregistered / Unconfirmed
GUEST, unregistred user!
两个都是备份与压缩不成功!!!

procedure TMainForm.ESimageFM12Click(Sender: TObject);
var
X: OleVariant;
sourcefiles,tempfiles,ole_tmp:string;
begin
dm1.ADO1.Connected:=False;
mainform.Dconnent.Connected:=False;
X:=createoleobject('JRO.JetEngine');
sourcefiles:=extractfilepath(application.exename)+'hy.mdb';
tempfiles:=extractfilepath(application.exename)+'temp.mdb';
StatusBar1.Panels[4].Text:='正在压缩数据...';
try
x.CompactDatabase(sourcefiles,tempfiles,ole_tmp,ole_tmp,';pwd=123');
deletefile(sourcefiles);
renamefile(tempfiles,sourcefiles);
StatusBar1.Panels[4].Text:='压缩数据完毕!';
if fileExists(sourcefiles) and FileExists(tempfiles) then
if deleteFile(sourcefiles)then
RenameFile(tempfiles,sourcefiles);
except
Messagedlg('主数据库正在被他人或应用程序使用,请稍后再试!',mtwarning,[mbok],0);
end;
dm1.ADO1.Connected:=True;
mainform.Dconnent.Connected:=True;
end;

procedure TMainForm.ESimageFM10Click(Sender: TObject);
var p,q:pchar;
begin
dm1.ADO1.Connected:=False;
mainform.Dconnent.Connected:=False;
if fileexists(extractfilepath(Application.ExeName)+'hy.mdb') then
begin
p:=pchar(extractfilepath(application.exename)+'backup.dat');
q:=pchar(StatusBar1.Panels[4].Text);
if CopyFile(p,q,true) then
Application.MessageBox('数据备份成功!','提示',MB_ICONINFORMATION + MB_OK)
else
Application.MessageBox('数据备份失败,请重新备份!','提示',MB_ICONINFORMATION + MB_OK);
end;
dm1.ADO1.Connected:=True;
mainform.Dconnent.Connected:=True;
end;
 
看一下我的:
function CompactMdb(const sDBName:string):Boolean;
const sProvider = 'Provider=Microsoft.Jet.OLEDB.4.0;';
var oJetEng: JetEngine;
sOldMdb, sNewMdb,sTempfile: string;
begin
ReSult:=True;
sTempFile:=ExtractFilePath(sDBname)+'Temp.mdb';
sOldMdb := sProvider + 'Data Source=' + sDBName+';'
+ 'Jet OLEDB:Database Password=HeQing;'
+'Jet OLEDB:Engine Type=4';//acces97:type=5:asscee2000

sNewMdb := sProvider + 'Data Source=' + sTempFile+';'
+ 'Jet OLEDB:Database Password=HeQing;'
+'Jet OLEDB:Engine Type=4';

try
try
oJetEng := CoJetEngine.Create;
oJetEng.CompactDatabase(sOldMdb, sNewMdb);
if FileExists(sTempFile) then
begin
CopyFile(Pchar(sTempFile),PChar(sDBName),False);
DeleteFile(sTempFile);
end;
except
on E: Exception do
ReSult:=False;
end
finally
oJetEng := nil;
end;
end;
 
后退
顶部