Logging your Sql with LINQ Update Commands. Simple Logging to your Visual Studio 2008 Debugger Output Console

So, you want to do an update but are wondering what the hec LINQ is doing.  Turns out it is really easy.  All you have to do is run in the debugger and add the Log option to your data context.

Here is an example:

DataClassesGeneralDataContext db3pLogicContext;
db3pLogicContext.Log = Console.Out;
 
var companyQuery = from tbl in db3pLogicContext.Companies
                   where tbl.ParentId != 0
                   select tbl;
 
int totalCntParents = companyQuery.Count();
foreach (DBAccess.Company co in companyQuery)
{
    co.CreateDate = DateTime.Now;
    break;
}
 
db3pLogicContext.SubmitChanges();
 

Now, when you run the code, you can look on your debugger output and see something that looks like this:

image
 

Also, if you want to actually execute select type statements, take a look at the Query Visualizer posted on Scott Guthrie’s web site:

http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx

About Peter Kellner

Peter is a software professional specializing in mobile and web technologies. He has also been a Microsoft MVP for the past 7 years. To read more about Peter Kellner and his experience click here. For information about how Peter Kellner might be able to help you with your project click here.

Follow me:


Follow

Get every new post delivered to your Inbox

Join other followers: