procedure TForm13.cxButton1Click(Sender: TObject); //数据库备份
var
path:string;
IsExists:Boolean;
begin
SaveDialog1.FileName:='GDZC_'+ DateToStr(NOW); //设置显示名称
if SaveDialog1.Execute then
if SaveDialog1.FileName <> '' then
begin
SaveDialog1.InitialDir:=ExtractFilePath(Application.ExeName); //设置路径
path := ExtractFilePath(application.ExeName); //程序路径
IsExists:= FileExists(PChar(SaveDialog1.FileName+'.mdb'));
//ShowMessage(PChar(SaveDialog1.FileName+'.mdb'));
if IsExists then
begin
Application.MessageBox(PChar('已经有同名数据库【'+savedialog1.FileName+'.mdb】存在,备份失败!'
+ #13#10 + ' 请更名后再试。'), '错误', MB_OK + MB_ICONSTOP);
Exit;
end
else
try
copyfile(pchar(path+ 'gdzc.mdb'), pchar(SaveDialog1.FileName+'.mdb'), True); //true 如何存在覆盖
Application.MessageBox(PChar('数据库已经备份为【'+savedialog1.FileName+'.mdb】'
+ #13#10#13#10+' 备份成功!'), '提示', MB_OK);
except
showmessage('数据库备份失败,请重试!');
end;
end;
end;
procedure TForm13.cxButton2Click(Sender: TObject); //恢复数据
var
path:string;
begin
if Application.MessageBox(' 是否真的恢复备份数据!' + #13#10#13#10 +
'恢复备份数据后现有数据将无法恢复。' + #13#10#13#10 + '是否真的恢复,请确认操作!', '提示',
MB_YESNO + MB_ICONQUESTION + MB_DEFBUTTON2) = IDyes then
begin
OpenDialog1.InitialDir:=ExtractFilePath(application.ExeName);
if OpenDialog1.Execute then
if OpenDialog1.FileName <> '' then
begin
path := ExtractFilePath(application.ExeName);
try
dm.con1.Connected := false;
RenameFile(path+'gdzc.mdb','GDZC恢复前备份.mdb');
copyfile(pchar(OpenDialog1.FileName),pchar(path + 'GDZC.mdb'), false);
showmessage('成功恢复数据!');
//Application.Terminate;
except
showmessage('恢复失败,请重试!');
end;
end;
end;
end;