用一个AdoConnection就可以了,当要备份数据库的时候将AdoConnection连到其它数据库试试吧。

解决方案 »

  1.   

    用一个ADOConnection1,我都搞不清这连接那,那连接这的。条件:
      ADOConnection1 link to "master" db
      ADOQuery1's Connection is ADOConnection1使用数据库函数
    procedure SetCurrentDB(const DBName: string);
    begin
      with ADOQuery1 do
      try
        if Active then Close;
        SQL.Text := 'use ' + DBName;
        ExecSQL;
      except
        raise....    
      end;
    end;//通过SetCurrentDB(DBName);就可以切换Current的DBName的连接。Backup:procedure BackupDB(const DBName, FileName: string);
    begin
      SetCurrentDB('master');
      with ADOQuery1 do
      try
        if Active then Close;
        SQL.Text := Format('backup database %s to disk=''%s''', [DBName, FileName]);
        ExecSQL;
      except
        raise ...
      end;      
    end;Restore DB:
    procedure RestoreDB(const DBName, FileName: string);
    begin
      if not FileExists(FileName) then Exit;
      SetCurrentDB('master');
      with ADOQuery1 do
      try
        if Active then Close;
        //这里还可以通过sp_who,检查一下是否有连接到DBName的连接者,你看有没需要,自己写吧
        SQL.Text := Format('restore database %s from disk=''%s''', [DName, FileName]);
        ExecSQL;
      except
        raise ...
      end;
    end;
      

  2.   

    ??????????????????????????????????????  以上不管采用谁的都出现“Database In Use....”之错误  ??????????????????????????????????????
      

  3.   

    不会吧,老兄,你是不是一步步按上面做的,if True then 帮你up else 再试
      

  4.   

    我也出现过这样的问题。看来是ADO的后台模块没关闭所至!