如何记录ADO连接的执行语句 [英] How to log ADO connection's execute statements

查看:108
本文介绍了如何记录ADO连接的执行语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using ado connection to connect to sql 2008 from inno, and i would like to know if we can log the details into a file so that can capture the errors thrown by sql.

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

Note: Thru ado connection I am not just executing select querys, i am using ado connection to execute set of statements to create database, procedure, tables etc.

推荐答案

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

For logging a database provider specific errors, use the Errors collection of the ADO Connection object. How to log these errors to a file, shows the following pseudo-script:

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连接的执行语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆