begin
 try
  with customerdata do
   begin
    datamain.Close;
    datamain.SQL.Clear;
    datamain.SQL.Add('use master');
    datamain.SQL.Add('ALTER DATABASE hangqian SET OFFLINE WITH ROLLBACK IMMEDIATE');
    datamain.SQL.Add('RESTORE DATABASE hangqian FROM DISK ='''+edit2.text+'''');
    datamain.SQL.Add('ALTER DATABASE hangqian SET ONLINE WITH ROLLBACK IMMEDIATE');
    datamain.SQL.Add('use hangqian');
    try
    datamain.ExecSQL;
    datamain.Close;
    except
    Application.MessageBox('还原数据库出错!','错误!',mb_ok+mb_iconError);
    end;
    MessageDlg('数据库还原成功!',mtConfirmation, [mbOK], 0);
    edit2.Text:='';
   end;
 except
 raise;
 end;

解决方案 »

  1.   

    这个SQL语句在查询分析器里执行可以恢复数据库,在DELPHI中执行就不行.也没有跳出错误.
      

  2.   

    trim(edit2.text);不知道你的路径是不是错了。下个断点跟踪看一下
    sql.add里面的代码
      

  3.   

    查询分析器里可以执行,这里应该也可以,不行用savetofile保存为.sql文件,在查询分析器中打开再执行,看行不行。
      

  4.   

    以上的方法我都试了,还是不行.执行到datamain.ExecSQL;就执行不下去了.也没跳出出错信息.
      

  5.   

    其实有个排它的问题.你要先断开数据连接.看我的代码.procedure TBak.btnRestoreClick(Sender: TObject);
    var
      Qry:TADOQuery;
    begin
      if FileExists(self.edtReStorePath.Text) then
        begin
          DM.Conn.Connected:=False; //断开系统数据库连接
          Qry:=TADOQuery.Create(self);
          Qry.CommandTimeout:=120;
          //把连接转移到master库
          Qry.ConnectionString:='Provider=SQLOLEDB.1;Password='+Login.GetDBServerInfo('Pwd')+';Persist Security Info=True;User ID='+Login.GetDBServerInfo('User')+';Initial Catalog=master;Data Source='+Login.GetDBServerInfo('Server');
          //-------------------端开mould库------------------------------------------
          Qry.Close;
          Qry.SQL.Clear;
          Qry.SQL.Text:='ALTER DATABASE rxd_CRM SET OFFLINE WITH ROLLBACK IMMEDIATE';
          Qry.Prepared;
          Qry.ExecSQL;
          //------------------- 端开mould库结束-------------------------------------
          Qry.Close;
          Qry.SQL.Clear;
          Qry.SQL.Text:='RESTORE DATABASE rxd_CRM FROM DISK='''+self.edtReStorePath.Text+''' with replace';
          Qry.Prepared;
          try
            Qry.ExecSQL;
            Login.MsgInfo('成功恢复数据!');
          finally
            //-------------------重新连接mould库------------------------------------
            Qry.Close;
            Qry.SQL.Clear;
            Qry.SQL.Text:='ALTER DATABASE rxd_CRM SET ONLINE WITH ROLLBACK IMMEDIATE';
            Qry.Prepared;
            Qry.ExecSQL;
            //-------------------重新连接mould库结束--------------------------------
            Qry.Free;
            DM.Conn.ConnectionString:=Login.GetDBServerInfo('DBStr');
            try
              DM.Conn.Connected:=True;
            except
              Login.MsgError('连接数据库失败,请尝试重启系统!');
            end;
          end
        end
      else
        begin
          Login.MsgInfo('数据文件不存在,请重新选择!');
          self.edtReStorePath.SetFocus;
        end;
    end;
      

  6.   

    忘记说了,GetDBServerInfo()这个函数我自己写的,作用是什么自己看.