7

Is there any way to in .NET to see what LINQ query against database we are firing? For eg. I am wring a query in LINQ and I want to see that what SQL query is firing to communicate with database.

Is there any Visual Studio window or any other way?

10
  • Which RDBMS do you use? Commented Mar 30, 2015 at 11:32
  • 1
    SQL Profiler, if you are using MSSQL. youtube.com/watch?v=mJ8Dyv4Uk6E Commented Mar 30, 2015 at 11:33
  • I am using SQL RDBMS Commented Mar 30, 2015 at 11:33
  • SQL RDBMS? Is this Microsoft, Oracle, MySQL, Postgre or...? Commented Mar 30, 2015 at 11:34
  • MSSQL, this is Microsoft Commented Mar 30, 2015 at 11:38

5 Answers 5

11

Are you looking for something like this

var context = new MyContext();
context.Database.Log = s => Debug.WriteLine(s);

Then whenever a query is executed, you'll see an output like :

var customers = context.Customers.ToList();

Opened connection at 30-3-2015 13:48:03 +02:00

SELECT [Extent1].[Guid] AS [Guid],
[Extent1].[FirstName] AS [FirstName],
[Extent1].[LastName] AS [LastName],
[Extent1].[Email] AS [Email],
[Extent1].[Created] AS [Created]
FROM [dbo].[Customer] AS [Extent1]

-- Executing at 30-3-2015 13:48:03 +02:00

-- Completed in 0 ms with result: SqlDataReader

Closed connection at 30-3-2015 13:48:03 +02:00

Sign up to request clarification or add additional context in comments.

5 Comments

And this works regardless what type of database is used.
Where I have to write this line, in particular class or in every class where want to check?
@Parveen You'll need to attach it to every context you instantiate. All the queries executed by that context will be logged. I added the context declaration line to the answer. Hope this helps
and where need to check the output in Console or Output Window?
@Parveen yes, the result is written into the Output window, from the "Show output from" list, select Debug.
5

If you have a DbContext on which you firing your LINQ queries you can simply set the DbContext.Database.Log property to something like this:

yourContext.Database.Log = (msg => System.Diagnostics.Debug.Write(msg, "SQL"));

After this, every SQL query shows up in the Debug console from your Visual Studio with the category SQL.

1 Comment

And this works regardless what type of database is used.
4

You can use SQL Profiler to see how the LINQ expression is translated into SQL statement.

Comments

3

You can use the Log property of DataContext object. Also, it depends on the type of application you are using.

For Web Application:-

db.Log = Response.Output;

For Console Application:-

db.Log = Console.Out;

Apart from this you can also use the GetCommand method of DataContext class. Sql Server Profiler is again an obvious option.

Comments

3

You can also use SQL Server Profiler, Trace or Extended Events. First two are deprecated.

https://technet.microsoft.com/en-us/library/bb630354(v=sql.105).aspx

https://msdn.microsoft.com/en-us/library/ms181091.aspx

https://technet.microsoft.com/en-us/library/ms191006(v=sql.105).aspx

1 Comment

The question was or any other way. So this answer is quite legitimate. Hello downvoter...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.