Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
Hi all,

I have a Microsoft SQL Server database and I am using LINQ to SQL to interact with it in my ASP.Net C# 4.0 Website. What I would like to do is have a method that before a query is submitted to the database, grabs the command that will be sent and allows me to look at it. Basically, I am trying to see how efficient my commands/requests are so that I can make more bigger requests rather than lots of small ones. I realise this can be done using Microsoft SQL Server tools but I don't actually have access the the database server itself, only debugging tools for my code.

I have tried overriding the SubmitChanges method of my DataContext to see if there was any way I could get at the outgoing commands from within that but I can't seem to find one. I also looked at the GetCommand method, but I don't want to have to go through all my 50,000 lines of code putting GetCommand methods on every LINQ statement in site... Furthermore, these would not show me the actual outgoing commands as they would include cached objects.

Is what I'm trying to do possible? Can anyone offer any insight into how to do it?

Thanks very much,
Ed
Posted
Comments
Manfred Rudolf Bihy 15-Feb-12 7:01am    
Interesting question! 5+
I hope someone will come up with viable solution. :)
Ed Nutting 15-Feb-12 7:04am    
Thanks :)
BobJanova 15-Feb-12 7:12am    
I'm not sure this is possible, but I will also watch this question with interest.
AmitGajjar 15-Feb-12 7:13am    
so, you can't use SQL Profiler nor you can not use DataContext.Log to check sql query , am i right ? or do you have one single class for executing DataContext query?
Ed Nutting 15-Feb-12 7:15am    
I can't use the SQL Profiler unfortunately but what is this DataContext.Log you mention? This may be what I have been looking for! As a web application it obviously has several (if not lots) of database contexts/connections but if there is built in logging in the DataContext I might be able to use that :)

1 solution

Thanks greatly to amitgajjar for this solution.

This solution use the in-built DataContext.Log property. Since in my situation I am running this as a website and so multiple DataContexts may exist, I used a static method to create all my DataContexts and assign to their Log property the static StringWriter I wanted to use for the log. The code looked as follows:

C#
public static class DatabaseHandler
{
    public static System.IO.StringWriter LogStream = new System.IO.StringWriter();

    internal static DatabaseDataContext CreateDatabaseContext()
    {
        DatabaseDataContext TheContext = new DatabaseDataContext();

        TheContext.Log = LogStream;

        return TheContext;
    }
}


And then I had an aspx page that simply set it's ouput to type "text" and did DatabaseHandler.LogStream.GetStringBuilder().ToString() to get the log.

This is a very efficient and neat solution, thanks so much again to amitgajjar.

Hope this helps anyone else in need,
Ed
 
Share this answer
 
Comments
AmitGajjar 15-Feb-12 7:43am    
Thanks Edward.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900