ado - 如何记录 ADO 连接的执行语句

标签 ado inno-setup

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

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

最佳答案

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

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;

关于ado - 如何记录 ADO 连接的执行语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13065500/

相关文章:

database - ADO - 如何获取主键字段

windows - inno setup创建的vb脚本快捷方式出错

inno-setup - Inno 安装编译器 : How to edit INI file and replace a value with {app} constant

sql-server - TClientDataSet 和大插入

excel - 如何从函数返回记录集

mysql - DELPHI 用 ADO 查询的结果填充一个列表框

inno-setup - 当 Inno Setup 安装失败时(在安装程序本身内)如何调用 exe?

inno-setup - 如何防止InnoSetup创建开始菜单快捷方式?

inno-setup - ArchitecturesAllowed Inno Setup 指令是否涉及 CPU 架构或操作系统架构?

DELPHI XE3 ADO 查询使我的应用程序在等待数据时卡住