在SQL SERVER 中建了一个数据库 我希望通过程序而不是SQL SERVER的企业管理器工具来操作备份指定的数据库中的几个表几个视图的内容。用DELPHI 能否实现 SQL SERVER 备份事先指定好数据库中的几个表几个视图的内容?或者是完全备份SQL SERVER 的指定数据库?请知道的朋友给出解决问题的具体方法。 谢谢

解决方案 »

  1.   

    备份:
    procedure TBackupFrm.BitBtn2Click(Sender: TObject);
    begin
      if Edit1.Text= '' then
        begin
          Showmessage('无选择要保存的文件名');
          exit;
        end;
      try
        try
          dmData.adoQryTmp.Active:= false;
          dmData.adoQryTmp.SQL.Clear;
          dmData.adoQryTmp.SQL.Add('BACKUP DATABASE [dzyl] TO DISK = '''+edit1.text+''' WITH INIT');
          dmData.adoQryTmp.ExecSQL;
        finally
          begin
            dmData.adoQryTmp.Active:= false;
            Showmessage('数据库备份成功!');
          end;
        end;
      except
      on e:exception do
        begin
          ShowMessage('数据库备份失败!');
        end;
      end;
    end;
    ---------------
    恢复
    procedure TBackupFrm.BitBtn4Click(Sender: TObject);
    begin
      if Edit2.Text = '' then
        begin
          showmessage('未选择要恢复的数据库文件!');
          exit;
        end;
      with dmData do
      begin
        try
          adocmmd.CommandText:='use master';
          adocmmd.Execute;
          adocmmd.CommandText:=' alter database dzyl set offline with rollback immediate';
          adocmmd.Execute;
          adocmmd.CommandText:='restore database dzyl from disk= '''+edit2.Text+''' with recovery ';
          adocmmd.Execute;
          adocmmd.CommandText:=' alter database dzyl set online with rollback immediate';
          adocmmd.Execute;
          showmessage('数据库恢复成功!');
          application.Terminate;
        except
        on e:exception do
          begin
            showmessage('数据库恢复失败!'+e.Message);
          end;    end;
      end;
      end;
    dmData.adoQryTmp连接的是系统MASTER数据库,备份还原之前应该关闭要备份还原的AdoConn数据库连接
    AdoConn.Connected:=False;
      

  2.   

    SQL数据库备份还原恢复 源码
    http://www.delphibox.com/article.asp?articleid=2890
      

  3.   

    象视图,是否可以单独备份?   假如SQL SERVER 服务的客户只允许你备份他们指定的几个表呢及一个视图呢, 能通过程序备份出来吗?
      

  4.   

    问题解决了吗?我想做个,每天自动定点备份数据库(比如说每天的12点自动备份SERVER数据库),有什么好的思路吗?
      

  5.   

    用一个Timer来做先用它跟踪计算机的时钟procedure Tmainfrm.Timer1Timer(Sender: TObject);
    begin
      StatusBar1.Panels[0].Text:=' 今天日期:'+FormatDateTime('yyyy"年"mm"月"dd"日"',now)+' 当前时间:'+FormatDateTime('hh"点"mm"分"ss"秒" ',now);
      //截获当前日期、时间
    end;再判断StatusBar1.Panels[0].Text里的值是不是与你指定的一致,如果是就执行备份程序!
    如果不是就不管了!
    if StatusBar1.Panels[0].Text="" then
    begin
      ......
    end
      

  6.   

    SQL不是可以设置自动定备份吗?为什么还要单独写程序?
      

  7.   

    能用delphi写一个恢复备分的吗?
      

  8.   

    用mssql做一个调度任务,每天晚上执行就可以了
      

  9.   

    //恢复
    procedure TForm_Restore.BitBtnRestoreClick(Sender: TObject);
    var
      restorePath: string;
      connStr,MySQL: string;
      TReg: TRegistry;
    begin
      if EditRestore.Text = '' then
      begin
        Application.MessageBox('请选择数据路径','提示!',mb_ok+mb_iconinformation);
        Exit;
      end;
      TReg := TRegistry.Create;
      TReg.RootKey := HKEY_LOCAL_MACHINE;
      try
        if TReg.OpenKey('\SOFTWARE\Microsoft\MSSQLServer\Setup',False) then
        begin
          restorePath := TReg.ReadString('SQLPath');
        end;
      finally
        TReg.CloseKey;
        TReg.Free;
        inherited;
      end;
      if restorePath = '' then
      begin
        Application.MessageBox('您为客户端,或者还没有安装SQL SERVER数据库,已经不能使用,请重新安装!','信息提示',mb_IconError+mb_ok);
        Exit;
      end;
      if Application.MessageBox('注意!恢复数据库会使现在的数据丢失,建议您先备份','问题?',mb_yesno+mb_iconquestion) = idyes then
      begin
        Screen.Cursor := crhourglass;
        Panel1.Visible := True;
        connStr := 'Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Password='+PassW+';Initial Catalog=master;Data Source='+OpIp+'';
        ADOCrestor.Connected := False;
        DM.ADOConn.Connected := False;
        ADOCrestor.ConnectionString := connStr;
        ADOCrestor.Connected := True;
        MySQL := 'ALTER DATABASE HSGZGL SET OFFLINE WITH ROLLBACK IMMEDIATE';
        with ADOQRestor do
        begin
          Close;
          SQL.Clear;
          SQL.Text := MySQL;
          try
            ExecSQL;
          except
            Application.MessageBox('数据库连接失败','错误!',mb_ok+mb_iconError);
            Exit;
          end;
        end;
        MySQL := 'Restore DataBase HSGZGL from disk=' + #39 + EditRestore.Text + #39 + ' with replace, ' +
                 ' move ' + #39 + 'HSGZGL_data' + #39 + ' to ' + #39 + restorePath + '\Data\HSGZGL_data.MDF' + #39 +
                 ', move ' + #39 + 'HSGZGL_log' + #39 + ' to ' + #39 + restorePath + '\Data\HSGZGL_data.LDF' + #39;
        with ADOQRestor do
        begin
          Close;
          SQL.Clear;
          SQL.Text := MySQL;
          try
            ExecSQL;
          except
            Screen.Cursor := crarrow;
            Panel1.Visible := False;
            Application.MessageBox('数据库恢复失败,请确保不在被使用中','错误!',mb_ok+mb_iconError);
            Exit;
          end;
        end;
        MySQL := 'ALTER DATABASE HSGZGL SET ONLINE WITH ROLLBACK IMMEDIATE';
        with ADOQRestor do
        begin
          Close;
          SQL.Clear;
          SQL.Text := MySQL;
          try
            ExecSQL;
          except      end;
        end;
        ADOCrestor.Connected := False;
        connStr := 'Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Password='+PassW+';Initial Catalog=HSGZGL;Data Source='+OpIp+'';
        DM.ADOConn.ConnectionString := connStr;
        DM.ADOConn.Connected := True;
        Screen.Cursor := crarrow;
        Panel1.Visible := False;
        Application.MessageBox('数据库恢复成功','恭喜!',mb_ok+mb_iconinformation);