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

Bogdan0x400
13558

edited Sep 27 '11 at 13:10


One Answer:

Hello,

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);
watcher.StartCapture();
QueryResult qr = q.Execute();
watcher.StopCapture();

foreach(DbCommandExecutionEventArgs item in watcher.DbCommandExecutionLog) {
  Console.WriteLine(item.CommandText);
}

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
22111211

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