使用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;
凡哥,别他妈吹牛逼了
分类:
Delphi相关
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)