1

Is there any way I can check how many DB queries a DataContext ran?

I know I can run an sql profiler but I want to analyze a big project with hundreds of queries.

If not, is there any useful event that I can register to and count them?

I'm looking for something like this:

using(DataContext ctx = new DataContext(connectionString))
{
    // Add load options
    // execute a query 

    // I want information about the number of actual sql queries that ran using this context. 
    // Adding "1 to many" loadoptions or complex queries can create multiple sub-queries and that's why I want this info.
}
4
  • Check out tools like: Miniprofiler miniprofiler.com Feb 25, 2015 at 8:57
  • @jessehouwing - Thanks for the tip. I'll check out the external package if there is no way to do this in house. I would expect the framework to provide me this information in one way or another. Feb 25, 2015 at 8:59
  • look at the code of miniprofiler to see how they do it :) github.com/MiniProfiler/dotnet/tree/master/… Feb 25, 2015 at 9:00
  • @jessehouwing - Nice..I'll check it out..Thanks! BTW I'm stuck with Linq2Sql.. Feb 25, 2015 at 9:02

1 Answer 1

3

In LINQ to SQL you have the option to log the generated SQL to a TextWriter. For instance you can log to the console:

ctx.Log = Console.Out;

This will not give you a count of queries but it will provide you with the actual SQL executed which probably gives you a better insight into what is going on.

If you want to provide an aggregated view of say the number of select statements you can log to a StringWriter and then use regular expressions to count the number of occurences of the word SELECT:

var stringWriter = new StringWriter();
ctx.Log = stringWriter;

// Use DataContext referenced by ctx ...

var regex = new Regex(@"\bSELECT\b");
var selectCount = regex.Matches(stringWriter.ToString()).Count;    

Obviously, you may have to also count other words like INSERT, UPDATE and DELETE and you may run into counting problems if you have say a WHERE clause containing the word SELECT.

Instead of trying to "understand" the SQL to make a count you can count the number of SQL statements executed. Each statement ends with a line that in my tests have the following format:

-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.34209

You can create a regular expression to match this string:

var regex = new Regex(@"^-- Context: ", RegexOptions.Multiline);
var statementCount = regex.Matches(stringWriter.ToString()).Count;    
5
  • Thanks for the answer. I know that I can actually see the queries. My problem is that If I load "1 to many" sub-tables, lots of sub queries can get executed. Will they get logged as well? Or will I see the same "main query" as I can see using ctx.ToString()? Feb 25, 2015 at 8:57
  • @AmirPopovich: All activities performed by the DataContext is logged using the current value of the Log property so you if you set this property before executing your main query and inspect the output after all your queries have completed you will see everything executed against the server. Feb 25, 2015 at 9:07
  • +1 Wow, that's pretty nice. I'll check this out later in my code. To bad they didn't provide a counter as well. Feb 25, 2015 at 9:10
  • @AmirPopovich: I have updated my answer to demonstrate how you can count the number of SQL statements executed which may be exactly what you are looking for. Feb 25, 2015 at 9:17
  • I've tested you code and it works great so thanks for that. I'm bummed that .net didn't add something built in and light since calculating this on each context is pretty heavy. Thanks!!! Feb 25, 2015 at 10:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.