views:

54

answers:

2

How to get the sql generated by LinqToSql for update method?

I use the following code to show the sql generated by LinqToSql in VS2008's debug output window, but it only gets the sql select method generated,

how can I find the sql update method that was generated by LinqToSql?

I know the Sql Server Profiler and LinqPad can get it(the sql-update generated), but I want to show them in VS2008 or Log them to a file.

public partial class Linq2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DemoDataContext ctx = new DemoDataContext ();

            // Then attach it to the Log property of your DataContext...
            ctx.Log = new DebugTextWriter();

            var product = ctx.Products.FirstOrDefault(); 

            product.ProductName = "NewName1";

            ctx.SubmitChanges();            

        }
    }

    // Add this class somewhere in your project...
    public class DebugTextWriter : System.IO.TextWriter
    {
        public override void Write(char[] buffer, int index, int count)
        {
            System.Diagnostics.Debug.Write(new String(buffer, index, count));
        }

        public override void Write(string value)
        {
            System.Diagnostics.Debug.Write(value);
        }

        public override Encoding Encoding
        {
            get { return System.Text.Encoding.Default; }
        }
    }

And I get the sql-select query in the VS2008 debug output window:

SELECT TOP (1) [t0].[Id], [t0].[ProductName] ……
FROM [dbo].[Products] AS [t0]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.30729.1
+1  A: 

Your Database context object has a Log method that you can override. Your full Update statement, and every SQL command generated by Linq-To-SQL, can be captured via this Log method. I know this works because I use it to capture all our queries in our app. Just keep in mind that L2S can send a fair amount of output to the Log method, so make sure to capture it all. Your Update statement is in there somewhere.

Randy Minder
Can you provide more details? I can not find the sql statement in the context.Log
Mike108
The context object has a System.IO.TextWriter method that you can attach a listener to. In my case, I've created a StringWriter class named SQLLogWriter and did the following: context.Log = SQLLogWriter. All L2S output will go to this writer.
Randy Minder
Could you provide the code? I used some StringWriter classes, and they only log the L2S's sql select statements. Are you sure that you can log the sql update statements to StringWriter class?
Mike108
Yes I am quite certain it captures everything. I just checked our Log file and I see 38 Update statements, about 300 Insert statements, and about 150 Delete statements. The Log method captures everything.
Randy Minder
Oh, that's great. But could you please provide that StringWriter class code?
Mike108
A: 

Thank you for all answers. I have found Linq To Sql Profiler to solve the problem.

Mike108