我写的代码不知道是怎么回事?只可以备份但不可以恢复。
请各位高手请教:
我的代码为。
var
   dir:string;
begin
  try
  if SaveDialog1.Execute then
     begin
        dir:=SaveDialog1.FileName+'.bak';
        With database.ADO_bb do
        begin
          Close;
          SqL.Clear;
          SQL.Add('backup database db_Client to disk='+''''+dir+'''');
          ExecSQL;
        end;
      showmessage('备份成功');
     end;
  except
  showmessage('备份失败');
  end;end;var
  dir:string;
begin
  try
    if OpenDialog1.Execute then
     begin
        dir:=OpenDialog1.FileName;        With database.ADO_bb do
        begin
          Close;
          SqL.Clear;
          SQL.Add('use master restore database db_Client from disk='+''''+dir+'''');
          ExecSQL;
          Close;
          SqL.Clear;
          SQL.Add('use db_Client ');
          ExecSQL ;
        end;        showmessage('恢复成功');
     end;
  except
    showmessage('恢复失败');
end;

解决方案 »

  1.   


    首先因该在,sql命令行下,将你的sql命令测试一下看能不能执行成功!
      

  2.   

    SQL.Add('use master restore database db_Client from disk='+quotedstr(dir));
      

  3.   

    procedure TForm_Restore.SpeedButtonOpenFileClick(Sender: TObject);
    begin
      OpenDialog.Title := '打开文件';
      if OpenDialog.Execute then
        EditRestore.Text := OpenDialog.FileName;
    end;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);
      end;