我用SQL Server 2000 作为数据库,数据库名为:Password_Test;Delphi 7 进行数据库备份和还原时遇到了这么个错误提示:
Project DB_Backup_Pro.exe raised exception class EOleException with message '第1行:'d:back1.bak'附近有语法错误。',
Process stopped. Use Step or Run to continue.
可是我检查了我的源程序很久没能找出来,但是在SQL Server 2000分析器中进行都成功。
源代码如下:procedure TDB_Backup.BitBtn2Click(Sender: TObject);
begin
  adoquery1.Close;
  adoquery1.ConnectionString:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=Password_Test;Data Source=Hello World';
  try
    with adoquery1 do
    begin
      close;
      sql.Clear;
      //还原数据库  
      sql.Add('restore database Password_Test from disk ="d:\back1.bak"');
      execsql;
    end;
    showmessage('还原成功。');
  except
  on e:exception do
  showmessage('还原失败。');
  end;
end;procedure TDB_Backup.BitBtn1Click(Sender: TObject);
var str:string;
begin
  adoquery1.Close;
  adoquery1.ConnectionString:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=Password_Test;Data Source=Hello World';
  try
    with adoquery1 do
    begin
      close;
      sql.Clear;
      //备份数据库
      sql.Add('backup database Password_Test to disk="d:\back1.bak" with init');
      execsql;
    end;
    showmessage('备份完成。');
  except
    on e:exception do
    showmessage('备份失败。');
  end;
end;

解决方案 »

  1.   

    USE Strutils;
    ...
    ...procedure TDB_Backup.BitBtn2Click(Sender: TObject);
    begin
      adoquery1.Close;
      adoquery1.ConnectionString:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=Master;Data Source=Hello World'; //此行改
      try
        with adoquery1 do
        begin
          close;
          sql.Clear;
          //还原数据库  
          sql.Add('restore database Password_Test from disk ='+Quotedstr('d:\back1.bak'));//此行改
          execsql;
        end;
        showmessage('还原成功。');
      except
      on e:exception do
      showmessage('还原失败。');
      end;
    end;procedure TDB_Backup.BitBtn1Click(Sender: TObject);
    var str:string;
    begin
      adoquery1.Close;
      adoquery1.ConnectionString:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=Password_Test;Data Source=Hello World';
      try
        with adoquery1 do
        begin
          close;
          sql.Clear;
          //备份数据库
          sql.Add('backup database Password_Test to disk='+Quotedstr('d:\back1.bak')+' with init'); //此行改
          execsql;
        end;
        showmessage('备份完成。');
      except
        on e:exception do
        showmessage('备份失败。');
      end;
    end;
      

  2.   

    Thank you very much !
    但是我有一点还要问一下为什么还原数据库的时候要将Initial Catalog改成Master呢??!
      

  3.   

    因为在还原时,该数据库不能有任何连接,必须独占,而ADO又一定要连接一个真实的数据库.所以,就选择一个SQL的系统数据库:Master.