LINQ in Action - LINQ Book & News

Viewing the SQL generated by DLinq

As demonstrated by Sahil in the last issue of his Demystifying DLinq series, you can get see the SQL DLinq would execute for a given query at debug-time using the built-in Query Visualizer:

This useful little tool can also run the SQL query and show you the results if you want.

In addition to the Query Visualizer, you can also get the SQL queries at runtime using the following line of code to redirect the SQL to the console: db.Log = Console.Out;
Of course, you can also redirect the logs to anything else as far as it's a System.IO.TextWriter.

Even better, you can also use GetQueryText(myQuery) on your DataContext instance to get the SQL at any time you want for a given query.
If you have updated in-memory objects returned by DLinq, you can get the SQL that would be executed on the database by using DataContext.GetChangeText().

A difference between GetQueryText() and GetChangeText() on one side, and the Query Visualizer on the other side, is that with the latter you can see the value of the SQL query's parameters.
Published Wednesday, June 7, 2006 4:15 PM by Fabrice Marguerie


No Comments
New Comments to this post are disabled