SQL Server 2000數(shù)據(jù)庫備份與恢復(fù)(DELPHI版本)
一、SQL語句說明
備份數(shù)據(jù)庫的SQL語句
BACKUP DATABASE 數(shù)據(jù)庫名 TO DISK = '文件名'
恢復(fù)數(shù)據(jù)庫的SQL語句
RESTORE DATABASE 數(shù)據(jù)庫名 FROM DISK = '文件名'
這里有個小技巧,我就經(jīng)常喜歡這么用。使用SQL Server 2000自帶的工具"SQL事件探查器",能夠監(jiān)視到所有執(zhí)行的SQL語句。
比如你不知道備份與恢復(fù)數(shù)據(jù)庫該用什么SQL,那么可以運行探查器,然后到企業(yè)管理器里執(zhí)行一下備份與恢復(fù)操作。
就可以到探查器里找找結(jié)果了。
有一點要說明的是,備份與恢復(fù),都只能在裝有數(shù)據(jù)庫的那臺機器(服務(wù)器)上操作,不能遠程執(zhí)行的。要特別注意這點。
二、備份部分
procedure TDataBackupForm.btnBackupClick(Sender: TObject);
var
ado:TADOCommand;
msg:TFMessageWindow;
begin
if IDYES=MessageDlg('數(shù)據(jù)庫備份功能只能在服務(wù)器上執(zhí)行,請先確認(rèn)計算機是要備份的服務(wù)器,是否現(xiàn)在進行備份?',mtConfirmation ,[mbYes,mbNo],0) then
begin
if SaveDialog1.Execute then
begin
msg := TFMessageWindow.Create(application);
msg.SetMessage('正在備份,可能需要幾分鐘時間。');
if (not FileExists(SaveDialog1.FileName)) or (DeleteFile(SaveDialog1.FileName)) then
begin
ado := TADOCommand.Create(nil);
ado.Connection := MainDM.ADOconn;
ado.CommandTimeout := 300;
msg.Show;
application.ProcessMessages;
ado.CommandText := 'BACKUP DATABASE WYSFXT TO DISK = ''' + SaveDialog1.FileName + '''';
try
ado.Execute;
except
ShowMessage('數(shù)據(jù)庫備份失敗');
Exit;
end;
ado.Free;
ShowMessage('操作成功,系統(tǒng)數(shù)據(jù)庫已備份。'+chr(13)+chr(13)
+'備份文件:'+SaveDialog1.FileName);
end;
msg.Close;
msg.Free;
end;
end;
end;
程序很簡單。可能要說明一下的是那個msg:TFMessageWindow;
這是一個窗體,自己新建個過去,重命名。再在上面加個LABEL,命名為lblMessage,增加一個函數(shù)
procedure TFMessageWindow.SetMessage(AStr:string);
begin
lblMessage.Caption := AStr;
end;
用于設(shè)置要顯示的信息。
在上面那段備份數(shù)據(jù)庫的代碼中,是調(diào)用這個窗體來顯示提示的!
三、恢復(fù)部分
恢復(fù)有個地方要注意的,是必須沒有其他程序連接到你要恢復(fù)的這個數(shù)據(jù)庫上。包括你的程序自身。
因為有可能你的程序前面已經(jīng)連接到數(shù)據(jù)庫,并進行了一些操作。比如:登錄系統(tǒng)->驗證用戶名。
這時必須關(guān)閉AdoConnection,并重新打開,打開的時候,注意不能指定默認(rèn)數(shù)據(jù)庫(特別注意)。你一指定默認(rèn)數(shù)據(jù)庫,就又連接上了。
那么永遠也沒辦法成功恢復(fù)數(shù)據(jù)庫的!
procedure TDataBackupForm.btnRestoreClick(Sender: TObject);
var
conn:TADOConnection;
ado:TADOCommand;
msg:TFMessageWindow;
sPath:string;
begin
if IDYES=MessageDlg('數(shù)據(jù)庫恢復(fù)功能只能在服務(wù)器上執(zhí)行,請先確認(rèn)計算機是要恢復(fù)的服務(wù)器,并且現(xiàn)在沒有任何程序正在使用數(shù)據(jù)庫,是否現(xiàn)在進行恢復(fù)?',mtConfirmation ,[mbYes,mbNo],0) then
begin
if OpenDialog1.Execute then
begin
msg := TFMessageWindow.Create(application);
msg.SetMessage('正在恢復(fù),可能需要幾分鐘時間。');
try
conn := TADOConnection.Create(nil);
conn.LoginPrompt := false;
conn.Connected := false;
conn.ConnectionString :=
'Provider=SQLOLEDB.1;User ID=sa;Password=sa;Data Source='
+ LogonServerForm.GetServerName;
MainDM.ADOconn.Free; //關(guān)掉原先已經(jīng)打開的數(shù)據(jù)庫連接
conn.Connected := true;
ado := TADOCommand.Create(nil);
ado.Connection := conn;
ado.CommandTimeout := 3000;
msg.Show;
Application.ProcessMessages;
sleep(100);
ado.CommandText := 'RESTORE DATABASE WYSFXT FROM DISK = '''
+ OpenDialog1.FileName + '''';
ado.Execute;
ado.Free;
msg.Close;
msg.Free;
msg := nil;
MessageDlg('數(shù)據(jù)庫恢復(fù)完成,現(xiàn)在將重啟動本系統(tǒng)。',mtInformation ,[mbOK],0);
MainForm.Close;
except
msg.Close;
msg.Free;
MessageDlg('數(shù)據(jù)庫恢復(fù)失敗,請確認(rèn)沒有其它程序已經(jīng)連接到數(shù)據(jù)庫?,F(xiàn)在將重啟動本系統(tǒng)。',mtInformation ,[mbOK],0);
MainForm.Close;
end;
sPath:=ExtractFilePath(application.ExeName);
shellexecute(0,'open',
Pchar(ExtractFileName(Application.ExeName)),nil,@sPath,sw_normal);
end;
end;
end;
注意程序最后用了個小技巧,使用shellexecute,在數(shù)據(jù)庫恢復(fù)成功,或者恢復(fù)失敗時,重啟程序本身。