?? backup_restore.pas
字號:
unit backup_restore;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ImgList, ComCtrls, FileCtrl, StdCtrls, Buttons, DB, ADODB,QFileCtrls,ShellAPi;
type
TForm1 = class(TForm)
Lab_Panel1: TLabel;
GroupBox1: TGroupBox;
Label1: TLabel;
Label2: TLabel;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
BitBtn1: TBitBtn;
GroupBox2: TGroupBox;
ListView1: TListView;
DriveComboBox1: TDriveComboBox;
DirectoryListBox1: TDirectoryListBox;
FileListBox1: TFileListBox;
StatusBar1: TStatusBar;
ImageList1: TImageList;
ADOConnection1: TADOConnection;
procedure BitBtn1Click(Sender: TObject);
procedure ListView1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure StatusBar1DrawPanel(StatusBar: TStatusBar;
Panel: TStatusPanel; const Rect: TRect);
procedure DriveComboBox1Change(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
Files:String;
public
{ Public declarations }
end;
var
Form1: TForm1;
backupflag:string;
implementation
{$R *.dfm}
procedure TForm1.BitBtn1Click(Sender: TObject);
var
conn:TADOConnection;
HF:TADOCommand;
BF:TADOQuery;
List:TListItem;
a:integer;
sPath,sFileName:String;
begin
if (RadioButton1.Checked=true)and (RadioButton2.Checked=false) then //備份數據
begin //1
if MessageDlg('您確認要備份數據嗎?',mtCustom,[mbYes,mbNo],0)=mrYes then
begin //2
if (ListView1.Items.Count<>0 )then //存在記錄
begin //3
For a:=0 to ListView1.Items.Count-1 do
begin //4
DeleteFile('children.bak');
end; //4 結束for循環
try
ListView1.Items.Clear;
BF:=TADOQuery.Create(Self);
BF.Connection:=adoconnection1;
BF.Close;
BF.SQL.Clear;
BF.SQL.Text:='Backup DataBase children to disk=:A';
if Copy(DirectoryListBox1.Directory,Length(DirectoryListBox1.Directory),1)='\' then
begin
BF.Parameters.ParamByName('A').Value:=DirectoryListBox1.Directory+Label2.Caption ;
end
else
begin
BF.Parameters.ParamByName('A').Value:=DirectoryListBox1.Directory+'\'+Label2.Caption;
end;
BF.ExecSQL;
BF.Close;
BF.Free;
adoconnection1.Connected:=false;
List:=ListView1.Items.Add;
List.ImageIndex:=0;
List.Caption:=Label2.Caption;
Lab_panel1.Caption:='備份完畢,舊備份已覆蓋';
Lab_panel1.Font.Color:=clWindowText;
except
MessageDlg('數據庫備份失敗,請確認程序是否連接到本機數據庫且用系統管理員身份登陸本系統。',mtInformation ,[mbOK],0);
exit;
end;
end //3
else //該目錄下沒有備份記錄
begin
// ADOConnection1.Connected:=false;
try
BF:=TADOQuery.Create(Self);
BF.Connection:=adoconnection1;
BF.Close;
BF.SQL.Clear;
BF.SQL.Text:='Backup DataBase children to disk=:A';
if Copy(DirectoryListBox1.Directory,Length(DirectoryListBox1.Directory),1)='\' then
BF.Parameters.ParamByName('A').Value:=DirectoryListBox1.Directory+Label2.Caption
else
BF.Parameters.ParamByName('A').Value:=DirectoryListBox1.Directory+'\'+Label2.Caption;
BF.ExecSQL;
BF.Close;
BF.Free;
List:=ListView1.Items.Add;
List.ImageIndex:=0;
List.Caption:=Label2.Caption;
Lab_panel1.Caption:='備份完畢';
Lab_panel1.Font.Color:=clWindowText;
except
MessageDlg('數據庫備份失敗,請確認程序是否連接到本機數據庫且用系統管理員身份登陸本系統。',mtInformation ,[mbOK],0);
exit;
end;
end;
end; //2
end; //1
if (RadioButton2.Checked=True) and (RadioButton1.Checked=false) then //選擇恢復數據
begin
if MessageDlg('您確認要恢復數據嗎?',mtCustom,[mbYes,mbNo],0)=mrYes then
begin
if ListView1.Items.Count=0 then //如果為空沒有記錄卡
begin
Lab_panel1.Caption:='對不起沒有發現備份卡,無法恢復數據';
Lab_panel1.Font.Color:=clred;
Exit;
End
else //如果存在記錄卡
begin
if Files='' then //沒有選擇恢復文件
begin
Lab_panel1.Caption:='對不起,請重新選擇要恢復的備份';
Lab_panel1.Font.Color:=clred;
Exit;
End
Else
begin //開始恢復
backupflag:='1';
try
conn:= TADOConnection.Create(nil);
conn.LoginPrompt:= false;
conn.Connected:= false;
conn.ConnectionString:='Provider=SQLOLEDB.1;Password=1234;Persist Security Info=True;User ID=sa;Initial Catalog=master;Data Source=(local)\children';
adoconnection1.Free; //關掉原先已經打開的數據庫連接
conn.Connected:= true;
HF:= TADOCommand.Create(nil);
HF.Connection:= conn;
HF.CommandTimeout := 3000;
sleep(100);
if Copy(DirectoryListBox1.Directory,Length(DirectoryListBox1.Directory),1)='\' then
sPath:=DirectoryListBox1.Directory+Files
Else
sPath:=DirectoryListBox1.Directory+'\'+Files;
HF.CommandText:= 'Restore DataBase children from disk='''+ sPath + '''';
HF.Execute;
HF.Free;
MessageDlg('數據庫恢復完成,現在將重新啟動本系統。',mtInformation ,[mbOK],0);
self.close;
except
MessageDlg('數據庫恢復失敗,請確認沒有其它程序已經連接到數據庫。現在將重新啟動本系統。',mtInformation ,[mbOK],0);
end;
sPath:=ExtractFilePath(application.ExeName);
sFileName:=ExtractFileName(Application.ExeName);
sPath:=spath+sFileName;
ShellExecute(handle,nil,PChar(sPath),nil,nil,SW_SHOW);
// frmmain.Close;
end;
end;
end;
end;
if (RadioButton2.Checked=false) and (RadioButton1.Checked=false) then
begin
Lab_panel1.Caption:='選擇錯誤,選項備份和恢復不能同時為空';
Lab_panel1.Font.Color:=clred;
exit;
end;
end;
procedure TForm1.ListView1Click(Sender: TObject);
var Lists:TListItem;
begin
Lists:=ListView1.Selected;
if Lists<>nil then //選擇了備份卡
begin
Files:=Lists.Caption;
End
else Files:='';
Lab_Panel1.Caption:='選擇要恢復的備份';
Lab_Panel1.Font.Color:=clWindowText;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
form1.Release;
form1:=nil;
end;
procedure TForm1.StatusBar1DrawPanel(StatusBar: TStatusBar;
Panel: TStatusPanel; const Rect: TRect);
begin
Lab_Panel1.Parent:=StatusBar1;
Lab_Panel1.Left:= Rect.Left+2;
Lab_Panel1.Height:=14;
Lab_Panel1.Top:= Rect.Top+2;;
end;
procedure TForm1.DriveComboBox1Change(Sender: TObject);
var
List_View:array[0..1]of TListItem;
a:integer;
begin
ListView1.Items.Clear; // 清空備份卡列表
if FileListBox1.Items.Count<>0 then
begin //添加當前路徑的備份
For a:=0 to FileListBox1.Items.Count-1 do
begin
List_View[0]:=ListView1.Items.Add;
List_View[0].Caption:=FileListBox1.Items.Strings[a];
List_View[0].ImageIndex:=0;
end;
end;
Lab_panel1.Caption:='位置選擇';
Lab_panel1.Font.Color:=clWindowText;
end;
procedure TForm1.FormShow(Sender: TObject);
var
List_View:array[0..1]of TListItem;
a:integer;
s:string;
begin
label2.Caption:='children.bak'; //顯示當天的備份信息
GetDir(3,s);
//ADOConnection1.Connected:=false; //在進行數據庫備份時要求中斷對數據庫的連接和操作
ListView1.Items.Clear;
if FileListBox1.Items.Count<>0 then
begin //顯示當前路徑下的備份
for a:=0 to FileListBox1.Items.Count-1 do
begin
List_View[0]:=ListView1.Items.Add;
List_View[0].Caption:=FileListBox1.Items.Strings[a];
List_View[0].ImageIndex:=0;
end;
end;
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -