为何用数据库中的所有任务中的备份可以成功,而恢复却总是说设备错误?
如何在delphi的程序中通过restore恢复,我用restore时总说数据库正在运行
有何解决良策?请各位大虾指教一下

解决方案 »

  1.   

    正在运行是你还没有关掉数据库啊!
    function RestoreDataBase(var Conn:TADOConnection;
                                SourceFile:string;
                                MainSourceFile:string;
                                TargetPath:string;
                                BackupInfo:TBackupInfo):Integer;
    var
      AdoCmd : TADOCommand;
    const
      RestoreSQL = 'RESTORE DATABASE [%s] FROM  DISK = %s WITH %s REPLACE,move %s to %s,move %s to %s';
      CommonWord = 'recovery, ';
      DeferentWord1 = 'FILE = 1,NORECOVERY, ';
      DeferentWord2 = 'FILE = 1,RECOVERY, ';
    begin
      Screen.Cursor:=crhourGlass;
      AdoCmd:=TADOCommand.Create(nil);
      try
        try
          AdoCmd.Connection:=Conn;
          if  BackupInfo.BackupType = 0 then
          begin
            AdoCmd.CommandText:=format(RestoreSQL,
                                      [BackupInfo.DBName,
                                      QuotedStr(SourceFile),
                                      CommonWord,
                                      QuotedStr(BackupInfo.DataFile),
                                      QuotedStr(TargetPath+BackupInfo.DataFile+'.mdf'),
                                      QuotedStr(BackupInfo.LogFile),
                                      QuotedStr(TargetPath+BackupInfo.LogFile+'.ldf')]);
            AdoCmd.Execute;
          end
          else
          begin
            AdoCmd.CommandText:=format(RestoreSQL,
                                      [BackupInfo.DBName,
                                      QuotedStr(MainSourceFile),
                                      DeferentWord1,
                                      QuotedStr(BackupInfo.DataFile),
                                      QuotedStr(TargetPath+BackupInfo.DataFile+'.mdf'),
                                      QuotedStr(BackupInfo.LogFile),
                                      QuotedStr(TargetPath+BackupInfo.LogFile+'.ldf')]
                                      );
            AdoCmd.Execute;
            AdoCmd.CommandText:=format(RestoreSQL,
                                      [BackupInfo.DBName,
                                      QuotedStr(SourceFile),
                                      DeferentWord2,
                                      QuotedStr(BackupInfo.DataFile),
                                      QuotedStr(TargetPath+BackupInfo.DataFile+'.mdf'),
                                      QuotedStr(BackupInfo.LogFile),
                                      QuotedStr(TargetPath+BackupInfo.LogFile+'.ldf')]);
             AdoCmd.Execute;
          end;
          result:=0;
        except
          on E: Exception do
          begin
            Application.MessageBox(Pchar(E.Message),'提示信息',{ E.HelpContext,}MB_ICONError+mb_OK);
            Result := -1;
          end;
      end;
      finally
        AdoCmd.Free;
        Screen.Cursor:=crdefault;
      end;
    end;
      

  2.   

    数据库恢复的时候应该先把数据库断开
    csdn上有不少这样的帖子 你可以搜索看看 肯定对你是由帮助的
      

  3.   

    简单:
    procedure TForm1.Button1Click(Sender: TObject);
    var
      query1:tadoquery;
      restorepath,databasename:string;
    begin
      restorepath:='e:\yl';//备份文件
      databasename:='mydata';
      query1:=tadoquery.Create(nil);
      try
          with query1 do
       Begin
         Connection := adoconnection1;//adoconnection1不用连任何数据库
         SQL.Add('Use Master');
         SQL.Add('Restore DataBase ' + DataBaseName);
         SQL.Add(' from disk = ' + '''' + RestorePath + '''');
         SQL.Add('with  recovery,Replace');
         try
           ExecSql;
         except
         end;
       end;  finally
        query1.Free;
      end;
    end;