views:

218

answers:

2

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
Thanks, I didn't know the Log property existed!
Lance Fisher
+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
I think that will do exactly what I want. Thanks!
Lance Fisher
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