How can I get SQL command text that is used to query the database from Query object?

asked Sep 27 '11 at 07:46

Bogdan0x400's gravatar image


edited Sep 27 '11 at 13:10

One Answer:


There are 2 primary ways of doing the task. First is to use DbCommandEventWatcherService like this:

watcher = (DbCommandEventWatcherService) session.GetService(typeof (DbCommandEventWatcherService));
Query q = session.CreateQuery(request.Text);
QueryResult qr = q.Execute();

foreach(DbCommandExecutionEventArgs item in watcher.DbCommandExecutionLog) {

The second way is to utilize System.Reflection and obtain value of QueryBase.command field.

answered Sep 29 '11 at 03:14

Dmitri%20Maximov's gravatar image

Dmitri Maximov

Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA