Is it possible to show the SQL that was executed for a particular request in an ASP.NET MVC app? I would like to show how many queries, and what they were on the bottom of pages during debugging.
+1
A:
The LINQ to SQL DataContext
class has a Log
property that you can use to obtain the generated SQL statement for the executes queries. To display the statement while debugging, see for example this: http://www.u2u.info/Blogs/Kris/Lists/Posts/Post.aspx?ID=11
Konamiman
2009-10-13 08:39:25
Thanks, I didn't know the Log property existed!
Lance Fisher
2009-10-13 16:36:38
+4
A:
If you really want to include it on the page, you can implement an Action Filter that logs the SQL output by using the Log
property on the Data Context, and then in OnResultExecuted
injects the SQL output into the View Data.
bzlm
2009-10-13 08:49:48
Don't forget to update this question with the code once you're done! =] The tricky part feels like getting at the Data Context from the Action Filter, so that you can start logging precisely in `OnActionExecuting` and stop precisely in `OnResultExecuted`.
bzlm
2009-10-14 06:29:22