11

This question already has an answer here:
view sql that linq-to-sql produces 3 Answers

I'm wondering if there is a way to see the T-SQL that was executed against the database in Visual Studio 2010 Ultimate when a LINQ to SQL query runs.

Community
  • 1
  • 1
l15a
  • 2,547
  • 5
  • 29
  • 41
  • Run SQL Sever profiler against that database and you can see what query is being executed. – Shyju Jun 12 '12 at 18:27

4 Answers4

24

If you have Visual Studio Ultimate, you can see every SQL query your application runs in the IntelliTrace window while debugging.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    Keep in mind that IntelliTrace is very resource-consuming. I usually have it disabled. – Mr. TA Jun 12 '12 at 18:32
  • @Mr.TA: Isn't that only Calls View? – SLaks Jun 12 '12 at 18:34
  • Maybe - I just disable the whole thing, makes me feel better. :) – Mr. TA Jun 12 '12 at 18:35
  • Thanks! I needed a quick easy way to view the SQL and this worked. – l15a Jun 12 '12 at 19:34
  • 2
    I do see queries in that window, but when I click to get more information I get a window that says `--The data may be truncated and may not represent the query that was run on the server`. The warning is correct, since the query in that window only has the name of the stored procedure and none of the information that I would find useful for debugging. – Isaac Lyman Oct 11 '16 at 16:55
  • Its not available for Microsoft VS Professional 2015 – Sandeep May 26 '17 at 18:11
11

You can use SQL Server Profiler to do that.

Mr. TA
  • 5,230
  • 1
  • 28
  • 35
6

You have basically two options:

1.) use a profiler, there's one free made by AnjLab http://anjlab.com/en/projects/opensource/sqlprofiler

2.) use LinqPad (again a free solution) http://www.linqpad.net/

You really don't need Ultimate VS or anything paid like some people already suggested...

walther
  • 13,466
  • 5
  • 41
  • 67
5

You could use the Log property of the DataContext.

db.Log = Console.Out;
var custQuery =
    from cust in db.Customers
    where cust.City == "London"
    select cust;

foreach(Customer custObj in custQuery)
    Console.WriteLine(custObj.CustomerID);
Andomar
  • 232,371
  • 49
  • 380
  • 404