使用TUniSQLMonitor监视SQL语句执行的耗时

复制代码
//代码来自官方论坛
type
TUniSqlMonitorEx = class(TUniSqlMonitor) private FExecuteDuration: Cardinal; FFinalEvent: Boolean; protected procedure InternalSQLExecute(Obj: TObject; const SQL: string; Params: TDAParams; const Caption: string; BeforeEvent: boolean; var MessageID: Cardinal); override; public property ExecuteDuration: Cardinal read FExecuteDuration; Property IsFinalEvent: Boolean read FFinalEvent; end; TForm1 = class(TForm) UniConnection1: TUniConnection; UniQuery1: TUniQuery; OracleUniProvider1: TOracleUniProvider; procedure FormCreate(Sender: TObject); private UniSQLMonitor: TUniSqlMonitorEx; procedure onSQL(Sender: TObject; Text: String; Flag: TDATraceFlag); { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} { TUniSqlMonitorEx } procedure TUniSqlMonitorEx.InternalSQLExecute(Obj: TObject; const SQL: string; Params: TDAParams; const Caption: string; BeforeEvent: boolean; var MessageID: Cardinal); begin if BeforeEvent then begin FFinalEvent := False; FExecuteDuration := GetTickCount; end; inherited; if not BeforeEvent then begin FFinalEvent := True; FExecuteDuration := (GetTickCount - FExecuteDuration); FOnSQLEvent(Obj, SQL, tfQExecute); end; end; procedure TForm1.FormCreate(Sender: TObject); begin UniSQLMonitor := TUniSqlMonitorEx.Create(Application.Owner); UniSQLMonitor.OnSQL := Self.onSQL; UniQuery1.Execute; end;
复制代码
复制代码
procedure TForm1.onSQL(Sender: TObject; Text: String; Flag: TDATraceFlag);
begin
  if (Sender is TUniQuery) and (Flag = tfQExecute) then
  begin
    if UniSQLMonitor.IsFinalEvent then
      ShowMessage(FloatToStr(UniSQLMonitor.ExecuteDuration / 100))
    else
      FCurrentSQL := Text;
  end
  else if (Flag = tfError) then
      ShowMessage(FCurrentSQL + ', Error:' + Text);
end;
复制代码

 

posted @   lybingyu  阅读(109)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示