在delphi中备份数据的方法

在delphi中备份数据的方法

unit DataBackUp;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, Buttons, StdCtrls, ADODB,StrUtils;

type
TfrmDataBack = class(TForm)
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
spdExit: TBitBtn;
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
Edit2: TEdit;
btnPath: TSpeedButton;
Label3: TLabel;
ProgressBar1: TProgressBar;
btnBackUp: TSpeedButton;
ADOCommand1: TADOCommand;
StatusBar1: TStatusBar;
OpenDialog1: TOpenDialog;
Label4: TLabel;
Edit3: TEdit;
btnChoice: TSpeedButton;
Label5: TLabel;
ProgressBar2: TProgressBar;
btnRestore: TSpeedButton;
procedure btnBackUpClick(Sender: TObject);
procedure btnPathClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure btnChoiceClick(Sender: TObject);
procedure btnRestoreClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
frmDataBack: TfrmDataBack;

implementation

uses main,myDataSource,GetDirectory;
{$R *.dfm}

procedure TfrmDataBack.btnBackUpClick(Sender: TObject);
var
database,myFileName:String;
begin
if Trim(Edit1.Text)='' then
begin
Application.MessageBox('备份前请先确定数据库要备份至哪个文件夹!','请确认',MB_OK+MB_IconInformation);
Edit1.SetFocus;
Exit;
end;
StatusBar1.SimpleText:= ' 正在备份中...';
ProgressBar1.Max := 100;
ProgressBar1.Min := 0;
ProgressBar1.Position := 0;
ProgressBar1.Step := 20;
database:='你的数据库名';
myFileName:=Trim(Edit1.Text)+Trim(Edit2.Text);
Screen.Cursor:=crHourGlass;
mDataModule.ADOConnection1.Close;
begin
try
ADOCommand1.CommandText := 'use Master';
ProgressBar1.StepIt;
Application.ProcessMessages;
ADOCommand1.Execute;
ADOCommand1.CommandText := 'execute sp_helpdevice';
ProgressBar1.StepIt;
Application.ProcessMessages;
ADOCommand1.Execute;
ProgressBar1.stepit;
ADOCommand1.CommandText := 'backup database ' + database + ' to disk=''' + myFileName + ''' with init';
ProgressBar1.StepIt;
Application.ProcessMessages;
ADOCommand1.Execute;
Application.ProcessMessages;
ADOCommand1.CommandText := 'Use ' + database;
ProgressBar1.StepIt;
ADOCommand1.Execute;
Application.MessageBox(pchar('数据库已经成功备份到 ' + myFileName + '!可删除以前的备份文件'), '提示', mb_ok + mb_iconinformation);
StatusBar1.SimpleText := '';
ProgressBar1.Position := 0;
try
mDataModule.ADOConnection1.Connected := True;
except
begin
StatusBar1.SimpleText := '';
application.MessageBox('无法重新接连数据库!', '提示', mb_ok + mb_iconerror);
abort;
end;
end;
self.Close;
except
on Exception do
if mDataModule.ADOConnection1.InTransaction then
mDataModule.ADOConnection1.RollbackTrans;
end;
end;
Screen.Cursor:=crArrow;
end;

procedure TfrmDataBack.btnPathClick(Sender: TObject);
var
myPath:String;
begin
if frmGetDirectory.ShowModal = mrOk then
begin
myPath:=Trim(frmGetDirectory.Edit1.Text);
if RightStr(myPath,1)='/' then
Edit1.Text:=myPath
else
Edit1.Text:=myPath+'/';
end;
end;

procedure TfrmDataBack.FormShow(Sender: TObject);
begin
PageControl1.ActivePageIndex:=0;
Edit1.Text:='';
Edit2.Text:='备份' + FormatDateTime('yyyy-mm-dd', frmMain.SysDate)+'.BAK';
Edit3.Text:='';
StatusBar1.SimpleText:='';
end;

procedure TfrmDataBack.btnChoiceClick(Sender: TObject);
begin
with OpenDialog1 do
begin
Title:='文件选择';
if execute then
Edit3.Text:=OpenDialog1.FileName;
end;
end;

procedure TfrmDataBack.btnRestoreClick(Sender: TObject);
var
database,myFileName:String;
begin
if Trim(Edit3.Text)='' then
begin
Application.MessageBox('请确定要恢复的备份文件名!','请确认',MB_OK+MB_IconInformation);
Edit3.SetFocus;
Exit;
end;
if Application.MessageBox('请确保无其他程序(如打开了企业管理器),正在使用本数据库,否则,还原无法进行!','请确认',mb_okcancel+mb_iconinformation)=idcancel then
begin
Abort;
end;
StatusBar1.SimpleText:= ' 正在恢复备份中...';
ProgressBar2.Max := 100;
ProgressBar2.Min := 0;
ProgressBar2.Position := 0;
ProgressBar2.Step := 20;
database:='工商银行客户管理';
myFileName:=Trim(Edit3.Text);
try
Screen.Cursor:=crHourGlass;
if mDataModule.ADOConnection1.Connected then
mDataModule.ADOConnection1.Close;
sleep(1000);
ProgressBar2.StepIt;
ADOCommand1.CommandText := 'use Master';
ADOCommand1.Execute;
sleep(600);
ProgressBar2.StepIt;
adocommand1.CommandText := 'execute sp_helpdevice';
adocommand1.Execute;
sleep(600);
ProgressBar2.stepit;
adocommand1.CommandText := 'Restore database ' + database + ' From disk=''' + myFilename + ''' with replace';
sleep(600);
ProgressBar2.StepIt;
try
adocommand1.Execute;
except
begin
raise exception.Create(syserrormessage(getlasterror));
abort;
end;
end;
adocommand1.CommandText := 'Use ' + database;
sleep(600);
ProgressBar2.StepIt;
adocommand1.Execute;
StatusBar1.SimpleText := '';
application.MessageBox(pchar('数据库已经成功还原到数据库 ' + database + '!'), '提示', mb_ok + mb_iconinformation);
ProgressBar2.Position := 0;
try
mDataModule.ADOConnection1.Connected := True;
except
begin
application.MessageBox('无法重新接连数据库!', '提示', mb_ok + mb_iconerror);
abort;
end;
end;
except
Application.MessageBox('有别的程序(如企业管理器)正在使用本数据库,数据无法恢复','提示',MB_OK+MB_IconInformation);
end;
StatusBar1.SimpleText := '';
frmMain.GetReportDate;
Screen.Cursor:=crArrow;
Self.Close;
end;

end.

【本贴仅代表作者个人意见,版权