f_DataBack.pas
上传用户:lcqxtx
上传日期:2013-02-15
资源大小:530k
文件大小:5k
- unit f_DataBack;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, ComCtrls, StdCtrls, Buttons, ExtCtrls;
- type
- TDataBack = class(TForm)
- PanelBkGnd: TPanel;
- Label5: TLabel;
- Label6: TLabel;
- Panel_button: TPanel;
- Bevel1: TBevel;
- BackData: TBitBtn;
- RecoverData: TBitBtn;
- CancelBtn: TBitBtn;
- Edit_path: TEdit;
- BrowseBtn: TButton;
- OpenDialog1: TOpenDialog;
- a1: TAnimate;
- procedure BrowseBtnClick(Sender: TObject);
- procedure Edit_pathChange(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure BackDataClick(Sender: TObject);
- procedure CancelBtnClick(Sender: TObject);
- procedure RecoverDataClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- B_showmessage: boolean;
- end;
- var
- DataBack: TDataBack;
- implementation
- uses f_SelectDir, data_1;
- {$R *.dfm}
- procedure TDataBack.BrowseBtnClick(Sender: TObject);
- begin
- Application.CreateForm(TSelectDir, SelectDir); //显示路径选择窗体
- SelectDir.DriveComboBox1.Text := ExtractFileDrive(Edit_path.Text);
- if (FileExists(Edit_path.Text)) then //检测路径是否存在
- SelectDir.DirectoryListBox1.Directory := Edit_path.Text;
- if (SelectDir.ShowModal = mrOK) then //当返回值为mrOK时,得到选择的路径
- Edit_path.Text := SelectDir.Label3.Caption + 'ERP' +
- formatdatetime('yyyymmdd', date) + '.back';
- SelectDir.free;
- end;
- procedure TDataBack.Edit_pathChange(Sender: TObject);
- begin
- if trim(Edit_path.Text) <> '' then
- BackData.Enabled := True
- else
- RecoverData.Enabled := False; //当路径框中为空时候, 恢复数据不可用
- end;
- procedure TDataBack.FormCreate(Sender: TObject);
- var
- SourcePath: string;
- begin
- SELF.Caption := application.Title + ' - 数据备份';
- B_showmessage := true;
- SourcePath := ExtractFileDir(Application.ExeName);
- //取得应用程序路径
- if (StrLen(PChar(SourcePath)) <> 3) then
- SourcePath := SourcePath + '';
- Edit_path.Text := SourcePath + 'BACKUPDATAERP' + formatdatetime('yyyymmdd',
- date) + '.back';
- //设置路径名+文件名
- end;
- procedure TDataBack.BackDataClick(Sender: TObject);
- begin
- self.Caption := '正在备份数据....';
- a1.Active := true;
- data1.cback.CommandText := 'backup database erp to disk=''' + Edit_path.Text +
- ''''; //备份数据库命令语句
- try
- data1.cback.Execute;
- // 执行备份
- showmessage(' 备份成功!');
- a1.Active := false;
- except
- showmessage(' 备份失败!');
- a1.Active := false;
- end;
- self.Caption := '数据备份和恢复';
- end;
- procedure TDataBack.CancelBtnClick(Sender: TObject);
- begin
- ModalResult := mrCancel;
- end;
- procedure TDataBack.RecoverDataClick(Sender: TObject);
- var
- DataPath: string;
- begin
- self.Caption := '正在恢复数据....';
- try
- data1.database1.Close;
- data1.database1.Connected := false;
- except
- MessageBox(0, '关闭数据库错误', '错误', MB_OK + MB_ICONSTOP + MB_TOPMOST);
- end;
- OpenDialog1.Filter := '备份文件 (*.back)|*.back|所有文件 (*.*)|*.*';
- //设置备份文件后缀
- OpenDialog1.InitialDir := ExtractFileDir(Application.ExeName) + 'backupdata';
- //设置备份路径
- if OpenDialog1.Execute then
- DataPath := OpenDialog1.FileName;
- if DataPath <> '' then
- begin
- if
- application.MessageBox('此操作将使上次备份以来的所有数据丢失,是否继续?',
- '恢复数据', MB_OKCANCEL) = idOK then
- begin
- a1.Active := true;
- data1.backconn.Connected := True;
- data1.cback.Connection := data1.backconn;
- data1.cback.CommandText :=
- 'ALTER DATABASE erp SET OFFLINE WITH ROLLBACK IMMEDIATE'; //切断连接
- data1.cback.Execute;
- data1.cback.CommandText := 'restore database erp from disk=''' + DataPath +
- '''' + ' with replace'; //恢复数据命令语句
- try
- try
- data1.cback.Execute;
- //执行恢复过程
- showmessage(' 恢复成功!');
- except
- showmessage('数据库正在被使用!请确定已关闭其它使用该数据库程序!');
- showmessage(' 恢复失败!');
- end;
- finally
- a1.Active := false;
- data1.cback.CommandText :=
- 'ALTER DATABASE erp SET ONLINE WITH ROLLBACK IMMEDIATE'; //重新连接数据库
- data1.cback.Execute;
- // 关闭临时数据库
- data1.backconn.Connected := False;
- // 打开数据库
- { data1.ClientFamily.CommandText := 'select * from ClientFamilyTable'; }
- data1.database1.Connected := True;
- try
- // 打开各个数据表是否有错误
- // data1.adoreader.Open;
- data1.ado_tab.Open;
- // data1.adobooks.Open;
- { adodm.Toper.Open;
- adodm.Tuser.Open;
- adodm.ZHBX.Open;
- adodm.Model.Open;
- adodm.ClientFamily.Open;
- adodm.ChargeItem.Open;
- adodm.ClientRoom.Open;
- adodm.Charge.Open;
- adodm.ClientPay.Open;
- adodm.ClientAdvance.Open; }
- except
- showmessage('程序运行发生不可预知错误,请重新启动程序!');
- //结束该程序
- Application.Terminate;
- end;
- end;
- end;
- end;
- self.Caption := '数据备份和恢复';
- end;
- end.