views:

102

answers:

8

I'm using Linq To Sql now, and how can I know how many queries was used to show the current page?

thank you very much

+1  A: 

I believe you will need to keep an internal counter to keep track.

Pino
+4  A: 

The DataContext contains a TextWriter to write the generated SQL to if thats any help to you

DataContext.Log = System.Console.Out;

will output the generated SQL to the console

David G
so is there anyway i can count like: THis page total queries is: xxx ?
DucDigital
You could analyze the output or implement your own TextWriter to keep a count as suggested by Jan.
David G
+3  A: 

You can implement a textwriter and associate your new textwriter with the datacontext.log; when the datacontext tries to write you can update your counter.

Jan Jongboom
+4  A: 

I would use SQL Server Profiler.

TGnat
+1  A: 

If I were you, I will combine the DaveG solution(+1) with the Jan solution(+1).

You can put the association of the textwriter with the datacontext.log in the constructor of the DataContext, and use the #if preprocessor directive like this:

#if DEBUG
    DataContext.Log = yourTextWriter.out;
#endif

This way will improve your performance in release mode.

Jonathan
+2  A: 

You may want to try out my Linq-to-SQL profiler. You can read more, download it and get a free 45-day trial license from http://www.huagati.com/L2SProfiler/

It give you not only what queries were executed, but what code triggered them, what the I/O cost, db-side timings, even the db side execution plan. You can use it both during development and in production environments to log and profile L2S queries...

Also see this blog post: http://huagati.blogspot.com/2009/06/profiling-linq-to-sql-applications.html

KristoferA - Huagati.com
+1  A: 

I believe your best bet is write an HTTP module that would display the number of queries executed as well as the SQL that was generated at the bottom of each page. It's amazingly slick. I'm not going to write it out here for obvious reasons but pick up a copy of Steve Sanderson's book "Pro ASP.NET MVC Framework" and he walks you through it. The output looks like:


Executed 4 SQL queries

  1. SELECT ...

  2. UPDATE ...

  3. UPDATE ...

  4. UPDATE ...


DM
A: 

Use Linq2Sql Profiler.

Dmytrii Nagirniak
i like this program
DucDigital