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

Subscription:

Once you sign in you will be able to subscribe for any updates here

Tags:

×22

Asked: Sep 27 '11 at 07:46

Seen: 4,201 times

Last updated: Sep 29 '11 at 03:14

powered by OSQA