views:

1289

answers:

4

I'm using Linq to SQL. I have a DataContext against which I am .SubmitChanges()'ing. There is an error inserting the identity field, and I'd like to see the query it's using to insert this identity field.

I don't see the query itself within the quickwatch; where can I find it from within the debugger?

+2  A: 

Run SQL Profiler if you have it. It'll show all traffic to your database, including SQL command text.

geofftnz
Thanks; that works. However, I was hoping to see it from VS.
tsilb
SQL Profiler is handy for other reasons too though, eg: seeing the load your code is putting on the DB.
geofftnz
A: 

You can also configure your datacontext to output the queries into a file.

eglasius
+4  A: 

Lots of people have been writing their own "DebugWriter" and attaching it like so:

// Add this class somewhere in your project...
class DebugTextWriter : System.IO.TextWriter {
   public override void Write(char[] buffer, int index, int count) {
       System.Diagnostics.Debug.Write(new String(buffer, index, count));
   }

   public override void Write(string value) {
       System.Diagnostics.Debug.Write(value);
   }

   public override Encoding Encoding {
       get { return System.Text.Encoding.Default; }
   }
}

// Then attach it to the Log property of your DataContext...
myDataContext.Log = new DebugTextWriter()

This will output everything that Linq-to-Sql is doing into Visual Studio's debug window.

Portman
+4  A: 

Further to Portman's answer, if you're a console application it's as simple as:

myDataContext.Log = Console.Out;

HTH
Kev

Kev