FireDAC FDScript发生异常无法捕获的问题。

今天在调试程序时发现,如下红色标识代码执行时发生了错误(Project ABTAYServer.exe raised exception class EMSSQLNativeException with message '[FireDAC][Phys][ODBC][Microsoft][ODBC SQL Server Driver][SQL Server]INSERT 语句中列的数目小于 VALUES 子句中指定的值的数目。VALUES 子句中值的数目必须与 INSERT 语句中指定的列的数目匹配。'.),但是Except段无法捕获,百思不得其解,问ChatGPT,提示的不太对,但受其启发,最后发现设置FDScript.ScriptOptions.BreakOnError:=true;以后就可以正常捕获了,这真是一个大坑,很多执行要求数据一致性,原子性,他竟然默认有错误还能继续执行,还能提交事务,真是让人匪夷所思。

function TServerMethods1.ExcuteScript(Script:string): string;
var
  StrListScript:TStringList;
  retJson:ISuperObject;
begin
  StrListScript:=TStringList.Create;
  retJson:=SO();
  try
    StrListScript.Text:=Script;
    Conn.StartTransaction;
    try
      cmScript.ExecuteScript(StrListScript);
      Conn.Commit;
      retJson.S['Code']:='00';
      retJson.S['Msg']:='保存成功';
      result:=retJson.AsJSON();
    Except
      on E:Exception  do
      begin
        Conn.Rollback;
        retJson.S['Code']:='-1';
        retJson.S['Msg']:=E.message;
        result:=retJson.AsJSON();
      end;
    end;
  finally
    StrListScript.Free;
  end;
end;

  

posted @ 2023-04-13 16:59  deepblue7911  阅读(64)  评论(0编辑  收藏  举报