数据库备份按钮Button1的单击事件如下:procedure TForm1.Button1Click(Sender: TObject);begin if Edit1.Text<>'' then begin self.Caption := '正在备份数据库,请稍候....'; ADOCommand1.CommandText:= 'backup database HR to disk=''' + Edit1.Text + ''''; //备份数据库命令语句 try ADOCommand1.Execute; // 执行备份 MessageDlg('数据库备份成功!',mtInformation,[mbOK],0); Self.Close; except MessageDlg('数据库备份失败!',mtConfirmation,[mbOK],0); Self.Close; end; end else MessageDlg('请输入正确的备份路径及文件名!',mtConfirmation,[mbYes],0);end; 数据库恢复按钮Button2的单击事件如下:procedure TForm.Button2Click(Sender: TObject);begin if OpenDialog1.Execute then begin self.Caption := '正在还原数据库,请稍候....'; try ADOConnection1.Connected:=False;//关闭与要恢复的数据库连接 //打开master数据库(事先将ADOConnection2连接到Master数据库) ADOConnection2.Connected:=True; ADOCommand1.Connection:=ADOConnection2;//转移连接到数据库master ADOCommand1.CommandText:='ALTER DATABASE HR SET OFFLINE WITH ROLLBACK IMMEDIATE';//切断要恢复的数据库,”HR” 是你要恢复的数据库的名字! ADOCommand1.Execute; ADOCommand1.CommandText:='RESTORE DATABASE HR FROM DISK = ''' +opendialog1.FileName +''' WITH REPLACE';//恢复要恢复的数据库 try ADOCommand1.Execute; MessageDlg('数据库恢复完成!',mtInformation,[mbOK],0); except MessageDlg('数据库恢复失败!',mtConfirmation,[mbOK],0); end; finally ADOCommand1.CommandText:='ALTER DATABASE HR SET ONLINE WITH ROLLBACK IMMEDIATE'; //重新连接恢复好的数据库 ADOCommand1.Execute; ADOConnection2.Connected:=False;//关闭master数据库 ADOConnection1.Connected:=True;//打开与恢复数据库的连接 ADOCommand1.Connection:=ADOConnection1;//恢复连接到数据库 end; Self.Close; end;end; 我也是从网上搜的别人的,然后整理了一下,我做的东西里就是这么用的,可以实现! 你试一试!
////////备份////////////////////////// procedure TForm18.BitBtn1Click(Sender: TObject); begin adoquery1.Close; try with adoquery1 do begin close; sql.Clear; sql.add('backup database mdf to disk=''c:\back1.bak'''); execsql; end; showmessage('备份完成'); except on e:exception do showmessage('备份失败'); end; end;///////////////还原/////////////////////////// procedure TForm18.BitBtn2Click(Sender: TObject); begin adoquery1.Close; try with adoquery1 do begin close; sql.Clear; sql.add('restore database mdf from disk=''c:\back1.bak'''); execsql; end; showmessage('还原成功'); except on e:exception do showmessage('还原失败'); end; end;
我也是从网上搜的别人的,然后整理了一下,我做的东西里就是这么用的,可以实现!
你试一试!
procedure TForm18.BitBtn1Click(Sender: TObject);
begin
adoquery1.Close;
try
with adoquery1 do
begin
close;
sql.Clear;
sql.add('backup database mdf to disk=''c:\back1.bak''');
execsql;
end;
showmessage('备份完成');
except
on e:exception do
showmessage('备份失败');
end;
end;///////////////还原///////////////////////////
procedure TForm18.BitBtn2Click(Sender: TObject);
begin
adoquery1.Close;
try
with adoquery1 do
begin
close;
sql.Clear;
sql.add('restore database mdf from disk=''c:\back1.bak''');
execsql;
end;
showmessage('还原成功');
except
on e:exception do
showmessage('还原失败');
end;
end;