【问题标题】:How to log ADO connection's execute statements如何记录 ADO 连接的执行语句
【发布时间】:2012-10-25 09:26:02
【问题描述】:

我正在使用来自 inno 的 ado 连接到 connect to sql 2008,我想知道我们是否可以将详细信息记录到文件中,以便捕获 sql 引发的错误。

注意:通过 ado 连接我不只是执行选择查询,我正在使用 ado 连接来执行一组语句来创建数据库、过程、表等。

【问题讨论】:

  • 您的意思是如何记录 ADO 提供程序特定的错误?脚本执行期间发生的所有错误都可能被异常处理程序捕获,但您可以从连接对象提供程序中获取特定(更详细)的错误对象。这就是你想要的吗?

标签: ado inno-setup


【解决方案1】:

要记录特定于数据库提供程序的错误,请使用 ADO Connection 对象的 Errors 集合。如何将这些错误记录到文件中,显示以下伪脚本:

procedure ConnectButtonClick(Sender: TObject);
var
  I: Integer;  
  ADOError: Variant;
  ADOConnection: Variant;  
  ErrorLog: TStringList;
begin
  ErrorLog := TStringList.Create;
  try    
    try
      ADOConnection := CreateOleObject('ADODB.Connection');
      // open the connection and work with your ADO objects using this
      // connection object; the following "except" block is the common
      // error handler for all those ADO objects
    except
      // InnoSetup scripting doesn't support access to the "Exception" 
      // object class, so now you need to distinguish, what caused the
      // error (if ADO or something else); for this is here checked if
      // the ADO connection object is created and if so, if its Errors
      // collection is empty; if it's not, or the Errors collection is
      // empty, then the exception was caused by something else than a
      // database provider
      if VarIsEmpty(ADOConnection) or (ADOConnection.Errors.Count = 0) then
        MsgBox(GetExceptionMessage, mbCriticalError, MB_OK)
      else
        // the Errors collection of the ADO connection object contains
        // at least one Error object, but there might be more of them,
        // so iterate the collection and for every single Error object
        // add the line to the logging string list
        for I := 0 to ADOConnection.Errors.Count - 1 do
        begin
          ADOError := ADOConnection.Errors.Item(I);
          ErrorLog.Add(
            'Error no.: ' + IntToStr(ADOError.Number) + '; ' +
            'Source: ' + ADOError.Source + '; ' +
            'Description: ' + ADOError.Description          
          );
        end;      
    end;
  finally
    ErrorLog.SaveToFile('c:\LogFile.txt');
    ErrorLog.Free;
  end;
end;

【讨论】:

  • 谢谢@Tlama,我会尝试并尽快通知您。
  • 代码确实捕获了错误,但没有弹出任何错误,因此用户不会知道是否有任何问题。
  • 当然,因为您想记录它们。它只是一个伪代码,展示了如何仅记录特定于数据库提供者的错误(并且只向用户显示所有非数据库提供者特定的错误)。如果您还想向用户显示特定于 DB 提供程序的错误,请使用 MsgBox,但请注意不要烦人,因为 Errors 集合中可能存在多个错误。
  • 哦好的@Tlama,我会试试看我能做什么:)
猜你喜欢
  • 2013-06-28
  • 1970-01-01
  • 2012-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-19
  • 2014-08-17
  • 1970-01-01
相关资源
最近更新 更多