📄 databack.pas
字号:
unit DataBack;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Buttons, ComCtrls, FR_Ctrls, ADODB, DB;
type
TBackupform = class(TForm)
PanelBkGnd: TPanel;
Label6: TLabel;
a1: TAnimate;
OpenDialog1: TOpenDialog;
Panel_button: TPanel;
Panel1: TPanel;
Label3: TLabel;
Label4: TLabel;
Label1: TLabel;
Label2: TLabel;
Edit_path: TfrComboEdit;
spexit: TSpeedButton;
cmdBackup: TSpeedButton;
cmdRestore: TSpeedButton;
adoConRestore: TADOConnection;
adoCmd: TADOCommand;
qryTmp: TADOQuery;
procedure FormCreate(Sender: TObject);
procedure Edit_pathButtonClick(Sender: TObject);
procedure spexitClick(Sender: TObject);
procedure cmdBackupClick(Sender: TObject);
procedure cmdRestoreClick(Sender: TObject);
private
{ Private declarations }
public
B_showmessage: boolean;
{ Public declarations }
end;
var
Backupform: TBackupform;
implementation
uses SelectDir, datamodule1;
{$R *.dfm}
procedure TBackupform.FormCreate(Sender: TObject);
var
SourcePath: string;
begin
a1.Visible:=false;
B_showmessage := true;
SourcePath := ExtractFileDir(Application.ExeName);//取得应用程序路径
if (StrLen(PChar(SourcePath)) <> 3) then
SourcePath := SourcePath + '\';
//设置路径名+文件名
Edit_path.Text := SourcePath + 'BACKUP\fhE2' + formatdatetime('yyyymmdd', date) + '.dat';
if not DirectoryExists(SourcePath + 'BACKUP') then
ForceDirectories(SourcePath + 'BACKUP');
end;
procedure TBackupform.Edit_pathButtonClick(Sender: TObject);
begin
Application.CreateForm(TSelectDirform, SelectDirform); //显示路径选择窗体
SelectDirform.DriveComboBox1.Text := ExtractFileDrive(Edit_path.Text);
if (FileExists(Edit_path.Text)) then //检测路径是否存在
SelectDirform.DirectoryListBox1.Directory := Edit_path.Text;
if (SelectDirform.ShowModal = mrOK) then //当返回值为mrOK时,得到选择的路径
Edit_path.Text := SelectDirform.Label3.Caption + '\fhE2' + formatdatetime('yyyymmdd', date) + '.dat';
SelectDirform.free;
end;
procedure TBackupform.spexitClick(Sender: TObject);
begin
ModalResult := mrCancel;
end;
procedure TBackupform.cmdBackupClick(Sender: TObject);
begin
if trim(Edit_path.Text) = '' then
begin
Application.MessageBox('将要备份数据文件不能为空 !','提示',64);
exit;
end;
a1.Visible:=true;
self.Caption := '正在备份数据....';
a1.Active := true;
DataE2.adoCmd.CommandText := 'backup database fhE2 to disk=''' + Edit_path.Text + ''''; //备份数据库命令语句
try
DataE2.adoCmd.Execute;// 执行备份
Application.MessageBox('备份成功!','提示',64);
a1.Active := false;
a1.Visible:=false;
except
Application.MessageBox('备份失败!','提示',64);
a1.Active := false;
end;
self.Caption := '数据备份和恢复';
end;
procedure TBackupform.cmdRestoreClick(Sender: TObject);
var
DataPath, sqlstr: string;
begin
try
adoConRestore.Connected :=false;
adoConRestore.ConnectionString:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=master;Data Source=.';
adoConRestore.Provider:='SQLOLEDB.1';
adoConRestore.Connected :=true;
except
Application.MessageBox('连接数据错误!请联系管理员。','错误提示',64);
exit;
end;
if trim(Edit_path.Text) = '' then
begin
Application.MessageBox('待恢复的数据文件不能为空 !','提示',64);
exit;
end;
OpenDialog1.Filter := '备份文件 (*.dat)|*.dat|所有文件 (*.*)|*.*'; //设置备份文件后缀
OpenDialog1.InitialDir := ExtractFileDir(Application.ExeName) + '\BACKUP';
//设置备份路径
if OpenDialog1.Execute then
DataPath := OpenDialog1.FileName;
if DataPath <> '' then
begin
if application.MessageBox('此操作将使上次备份以来的所有数据丢失,是否继续?', '恢复数据', MB_OKCANCEL) = idOK then
begin
self.Caption := '正在恢复数据....';
DataE2.adoConStore.Close;
DataE2.adoConStore.Connected := false;
DataE2.adoConStore.Free;
a1.Active := true;
with qryTmp do
begin
close;
open;
end;
sqlstr:='restore database fhE2 from disk=''' + DataPath + '''' + ' with replace';
{,MOVE '+'''fhstore_Data'''+' TO '; //恢复数据命令语句
sqlstr:=sqlstr+''''+qryTmp.fieldbyname('filename').AsString+'''';
sqlstr:=sqlstr+', MOVE '+'''fhstore_log'''+' TO ';
sqlstr:=sqlstr+''''+qryTmp.fieldbyname('filename').AsString+''''; }
adoCmd.CommandText:=sqlstr;
try
//adoConRestore.BeginTrans;
adoCmd.Execute;//执行恢复过程
Application.MessageBox('恢复成功!','提示',64);
DataE2.gTableId:=-1;
a1.Active := false;
Application.MessageBox('数据恢复成功后需要重新启动程序!','提示',64);
//adoConRestore.CommitTrans;
//结束该程序
Application.Terminate;
except
//adoConRestore.RollbackTrans;
showmessage('数据库正在被使用!请确定已关闭其它使用该数据库程序!');
Application.MessageBox('恢复失败!','提示',64);
Application.Terminate;
end;
end;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -