sql server 备份

procedure TfrmDbBackup.BtnOKClick(Sender: TObject);
var
  sfile: string;
begin
  if trim(EditPath.Text) = '' then
   begin
      application.messagebox('请选择备份文件路径!', '系统提示', MB_ICONINFORMATION);
      exit;
   end;
    sfile:=EditPath.Text;
    if FileExists(sfile) then
    begin
      if application.messagebox('  文件已存在是否覆盖?  ','提示',MB_YESNO )=IDNo then
        exit
      else Deletefile(sfile);
    end;

   ProgressBar1.Visible:= true;
   ProgressBar1.Max:=80;
   ProgressBar1.Min:=0;
   ProgressBar1.Position:=0;
   ProgressBar1.Step:=20;

   frmDataModule.ADOConnection.Connected:= false;

    try
      with ADOQuery1 do
      begin
        //制作操作日志
        ProgressBar1.StepIt;
        SQL.Clear;
        SQL.Add('use Master');
        ExecSQL;
        ProgressBar1.StepIt;

        SQL.Clear;
        SQL.Add('execute sp_helpdevice');
        ExecSQL;
        ProgressBar1.StepIt;

        SQL.Clear;
        SQL.Add('backup database erp_0 to disk=:disk with init');
        Parameters.ParamByName('disk').Value:= sfile;
        ExecSQL;
        ProgressBar1.StepIt;
        //-------2008 02 26-----------
        SQL.Clear;
        SQL.Add('use erp_0');
        ExecSQL;
      end;
      if FileExists(sfile) then
      begin
        application.messagebox('  备份成功!  ', '提示', MB_ok);
        try
          frmDataModule.ADOConnection.Connected:=true;
        except
          application.messagebox('无法重新连接数据库,请重新登入软件!', '提示', MB_ok);
          //退出程序,自动释放相关的资源
          application.Terminate;
        end;
        close;
      end
      else begin
        with ADOQuery1 do
        begin
          try
            SQL.Clear;
            SQL.Add('use Master');
            ExecSQL;

            SQL.Clear;
            SQL.Add('exec  master..xp_cmdshell '+'''del disk=:disk''');
            Parameters.ParamByName('disk').Value:= sfile;
            ExecSQL;
          except
          end;

          application.messagebox('  备份失败!  ', '提示', MB_ok);
        end;
      end;
    except
      on exception do
      begin
        application.messagebox('  备份失败!  ', '提示', MB_ok);
      end;
    end;//try
    ProgressBar1.Visible:= false;
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值