在程序中怎樣實現數據的備份与恢復(SQL SERVER)??(50分)

  • 主题发起人 jesse.zhou
  • 开始时间
J

jesse.zhou

Unregistered / Unconfirmed
GUEST, unregistred user!
在程序中怎樣實現數據的備份与恢復(SQL SERVER)??
 
备份: Backup Database
恢复: Restore Database
具体看一下帮助吧, 很详细的
 
可否提供源嗎?
 
ADOConnection.execute('Backup Database .. to disk ..');
 
begin
ADOConnection1.Connected := False;
ADOCommand1.CommandText := 'BACKUP DATABASE DataBaseName TO DISK = ' + #39 + 'C:/KKK.BAK' + #39;
ADOCommand1.Execute;
ADOCommand1.CommandText := 'ALTER DATABASE DataBaseName SET OFFLINE WITH ROLLBACK IMMEDIATE';
ADOCommand1.Execute;
ADOCommand1.CommandText := 'RESTORE DATABASE DataBaseName FROM DISK = ' + #39 + 'C:/KKK.BAK' + #39;
ADOCommand1.Execute;
ADOCommand1.CommandText := 'ALTER DATABASE DataBaseName SET ONLINE WITH ROLLBACK IMMEDIATE';
ADOCommand1.Execute;
ADOConnection1.Connected := True;
ADOTable1.Active := true;
end;
 
procedure Tmainfm.N5Click(Sender: TObject);
var
filePath,filename:string;
Q_temp:TQuery;
begin
if MessageDlg('此操作是完全備份數據庫,避免程序發生錯誤時還原數據庫,使程序正常?',mtConfirmation,[MbYes,mbNo],0)=mrNo then exit;
SaveDialog1.Title:='建立或復蓋一個數據庫備份文件...';
SaveDialog1.InitialDir:='D:/Person';
SaveDialog1.Filter:='SQLDB(*.bak)|*.bak|All files(*.*)|(*.*)';
if SaveDialog1.Execute then
filepath:=SaveDialog1.FileName;
if copy(filepath,length(trim(filepath))-3,4)='.BAK' then
begin
if MessageDlg('備份將要復蓋這個文件?',mtConfirmation,[mbYes,mbNo],0)=mrNo then exit;
end
else filepath:=filepath+'.BAK';
if filepath='.BAK' then exit;
Filename:=ExtractFileName(SaveDialog1.FileName);
if F_status=nil then
F_status:=TF_status.Create(self);
F_status.Animate1.Visible :=true;
F_status.Animate1.Active :=true;
if MessageDlg('你將這個文件將備份為: '+filepath,mtConfirmation,[mbYes,mbNo],0)=mrNo then exit;
Q_temp:=TQuery.Create(self);
Q_temp.DatabaseName:='Person_DB';
try
F_status.Show;
F_status.Update;

with Q_temp do
begin
close;
sql.Clear;
sql.Add('exec backupdatabase @filepath='+''''+filepath+''''+
',@filename='+''''+filename+'''');
ExecSQL;
end;
F_status.Hide;
Showmessage('數據庫成功備份到: '+filepath);
finally
Q_temp.free;
F_status.Free;
end;



end;(備份)
procedure Tmainfm.N4Click(Sender: TObject);
var
i:integer;
Fromdate,todate:string;
Q_temp:TQuery;
begin
F_selectBK.Caption:='選擇恢復';
if MessageDlg('此操作是將先前的選擇備份數據還原,是否進行?',mtConfirmation,[mbYes,mbNo],0)=mrNo then exit;
if F_selectBK=nil then
F_selectBK:=TF_selectBK.Create(self);
if F_selectBK.ShowModal<>mrOK then exit;
if F_status=nil then F_status:=TF_status.Create(self);
F_status.Animate1.Visible :=true;
F_status.Animate1.Active :=true;
Fromdate:=F_selectBK.Maskedit1.text;
todate:=F_selectBK.maskedit2.text;
if messageDlg('日期是從: '+Fromdate+'到 '+todate,mtconfirmation,[mbYes,mbNo],0)=mrNo then exit;
Q_temp:=TQuery.Create(self);
Q_temp.DatabaseName:='Person_DB';
F_status.Label1.Caption:='還原備份進行中,稍候...';
F_status.Show;
F_status.Update;
for i:=0 to F_selectBK.CheckListBox1.Items.Count-1 do
if F_selectBK.CheckListBox1.Checked then
begin
if i=0 then
begin
with Q_temp do
begin
close;
sql.Clear;
sql.Add('exec OK1 @type='+''''+'Re'+''''+',@fromdate='+''''+fromdate+''''+
',@todate='+''''+todate+'''');
ExecSQL;
end
end
else if i=1 then
begin
with Q_temp do
begin
close;
sql.Clear;
sql.Add('exec OK2 @type='+''''+'Re'+''''+',@fromdate='+''''+fromdate+''''+
',@todate='+''''+todate+'''');
ExecSQL;
end
end;
end;
F_status.hide;
F_status.free;
Showmessage('操作成功完成!');


end;
(選擇還原)
 
这种操作只能备份在服务器上,若要备份到任意地方,还是行不通的。
 
顶部