Einzelnen Beitrag anzeigen

bernhard_LA

Registriert seit: 8. Jun 2009
Ort: Bayern
1.138 Beiträge
 
Delphi 11 Alexandria
 
#1

sql monitor für ADO

  Alt 22. Dez 2014, 10:03
Datenbank: MSSQL • Version: 12 • Zugriff über: ADO
ich habe eine Lösung für einen ADO SQL Monitor hier http://theroadtodelphi.wordpress.com...ler-using-ado/ gefunden.

In Abandlung zur Lösung oben möchte ich eine TListBox zur Anzeige der SQL Events verwenden, leider bekomme ich schon beim ersten Aufruf einen AV Fehler bei der Codezeile ListViewSQL.Items.BeginUpdate;




Delphi-Quellcode:

unit SQLMonitor;


interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  typinfo,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls,
  , Data.DB, Data.Win.ADODB, Vcl.Buttons;

type
  TWillExecuteEvent = procedure(const Connection: TADOConnection;
    var CommandText: WideString; var CursorType: TCursorType;
    var LockType: TADOLockType; var CommandType: TCommandType;
    var ExecuteOptions: TExecuteOptions; var EventStatus: TEventStatus;
    const Command: _Command; const Recordset: _Recordset) of object;

type
  TExecuteCompleteEvent = procedure(const Connection: TADOConnection;
    RecordsAffected: Integer; const Error: Error; var EventStatus: TEventStatus;
    const Command: _Command; const Recordset: _Recordset) of object;

type
  TSQLMonitorForm = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    Panel2: TPanel;
    Label1: TLabel;
    ClearButton: TButton;
    InfoBitBtn: TBitBtn;
    MonitorStatusBar: TStatusBar;
    Panel3: TPanel;
    SQLMsgMemo: TMemo;
    ListViewSQL: TListView;
    MonitorTimer: TTimer;
    procedure Button1Click(Sender: TObject);
    procedure ClearButtonClick(Sender: TObject);
    procedure ListViewSQLChange(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    procedure InfoBitBtnClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure MonitorTimerTimer(Sender: TObject);
  private
    { Private-Deklarationen }

    FAdoConnection: TADOConnection;
    procedure SetAdoConnection(const Value: TFlexConnection);
    procedure AddLog(const Command, CommandType, Status, CursorType,
      LockType: String; RecordsAffected: Integer);
    procedure ADOConnectionExecuteComplete(Connection: TADOConnection;
      RecordsAffected: Integer; const LastError: Error;
      var EventStatus: TEventStatus; const Command: _Command;
      const Recordset: _Recordset);
    procedure ADOConnectionWillExecute(Connection: TADOConnection;
      var CommandText: WideString; var CursorType: TCursorType;
      var LockType: TADOLockType; var CommandType: TCommandType;
      var ExecuteOptions: TExecuteOptions; var EventStatus: TEventStatus;
      const Command: _Command; const Recordset: _Recordset);
  public
    { Public-Deklarationen }

    property Connection: TADOConnection read FAdoConnection
      write SetAdoConnection;
  end;

var
  SQLMonitorForm: TSQLMonitorForm;

implementation

{$R *.dfm}

procedure TSQLMonitorForm.ADOConnectionWillExecute(Connection: TADOConnection;
  var CommandText: WideString; var CursorType: TCursorType;
  var LockType: TADOLockType; var CommandType: TCommandType;
  var ExecuteOptions: TExecuteOptions; var EventStatus: TEventStatus;
  const Command: _Command; const Recordset: _Recordset);

begin
  SQLMonitorForm.AddLog(CommandText,
    'Before ' + GetEnumName(TypeInfo(TCommandType), Integer(CommandType)),
    GetEnumName(TypeInfo(TEventStatus), Integer(EventStatus)),
    GetEnumName(TypeInfo(TCursorType), Integer(CursorType)),
    GetEnumName(TypeInfo(TADOLockType), Integer(LockType)), 0);

end;

procedure TSQLMonitorForm.ADOConnectionExecuteComplete
  (Connection: TADOConnection; RecordsAffected: Integer; const LastError: Error;
  var EventStatus: TEventStatus; const Command: _Command;
  const Recordset: _Recordset);

begin

  SQLMonitorForm.AddLog(Command.CommandText,
    'After ' + GetEnumName(TypeInfo(TCommandType), Integer(Command.CommandType)
    ), GetEnumName(TypeInfo(TEventStatus), Integer(EventStatus)),
    GetEnumName(TypeInfo(TCursorType), Integer(Recordset.CursorType)),
    GetEnumName(TypeInfo(TADOLockType), Integer(Recordset.LockType)),
    RecordsAffected);

end;

procedure TSQLMonitorForm.AddLog(const Command, CommandType, Status, CursorType,
  LockType: String; RecordsAffected: Integer);
var
  Item: TListItem;
begin
  ListViewSQL.Items.BeginUpdate;
  try
    Item := ListViewSQL.Items.Add;
    Item.Caption := FormatDateTime('DD/MM/YYYY HH:NN:SS.ZZZ', Now);
    Item.SubItems.Add(CommandType);
    Item.SubItems.Add(Command);
    Item.SubItems.Add(Status);
    Item.SubItems.Add(IntToStr(RecordsAffected));
    Item.SubItems.Add(CursorType);
    Item.SubItems.Add(LockType);
  finally
    ListViewSQL.Items.EndUpdate;
  end;

  ListViewSQL.Items.Item[ListViewSQL.Items.Count - 1].MakeVisible(false);
  // Scroll to the last line

  MonitorStatusBar.SimpleText := ' ***** ' + Item.Caption + ' ! ' +
    FormatDateTime('DD/MM/YYYY HH:NN:SS.ZZZ', Now);
end;

procedure TSQLMonitorForm.Button1Click(Sender: TObject);
begin
  SQLMonitorForm.Close;
end;

procedure TSQLMonitorForm.ClearButtonClick(Sender: TObject);
begin
  SQLMsgMemo.Clear;
end;

procedure TSQLMonitorForm.FormHide(Sender: TObject);
begin
  MonitorTimer.Enabled := false;
end;

procedure TSQLMonitorForm.FormShow(Sender: TObject);
begin
  MonitorTimer.Enabled := true;
end;

procedure TSQLMonitorForm.InfoBitBtnClick(Sender: TObject);
begin
  SQLMsgMemo.Lines.Clear;
  SQLMsgMemo.Lines.Add(datetostr(Now) + TimeTostr(Now));
  SQLMsgMemo.Lines.Add(FAdoConnection.ConnectionString);
  SQLMsgMemo.Lines.Add(Booltostr(FAdoConnection.Connected));
end;

procedure TSQLMonitorForm.ListViewSQLChange(Sender: TObject; Item: TListItem;
  Change: TItemChange);
begin
  if (ListViewSQL.Selected <> nil) then
    SQLMsgMemo.Lines.text := ListViewSQL.Selected.SubItems[1];

  // SQLMsgmemo.Lines.add (Item.subitems[1]);

end;

procedure TSQLMonitorForm.MonitorTimerTimer(Sender: TObject);
begin
  MonitorStatusBar.SimpleText := ' Date ' + datetostr(Now) + ' / ' +
    TimeTostr(Now);
end;

procedure TSQLMonitorForm.SetAdoConnection(const Value: TADOConnection);
begin
  FAdoConnection := Value;

  FAdoConnection.OnWillExecute := SQLMonitorForm.ADOConnectionWillExecute;

  FAdoConnection.OnExecuteComplete :=
    SQLMonitorForm.ADOConnectionExecuteComplete;

end;

end.




Aufruf wie folgt


Delphi-Quellcode:
var
  SQLMonitorForm: TSQLMonitorForm;
begin
  try
    SQLMonitorForm := TSQLMonitorForm.Create(self);
    SQLMonitorForm.Connection := MainDBConnection;
    SQLMonitorForm.Show;
  finally
    // SQLMonitorForm.free;
  end;

.

Geändert von bernhard_LA (22. Dez 2014 um 10:08 Uhr)
  Mit Zitat antworten Zitat