2

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

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

4

1 回答 1

2

要记录特定于数据库提供程序的错误,请使用ADO对象的Errors集合。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;
于 2012-10-25T15:33:44.403 回答