tags:

views:

793

answers:

3
      var newUser = new tblUser()
   { 
    Email = strEmail,
    Password = strPassword,
    DateBirth = DateTime.Parse(strDateBirth),
   };

   db.tblUsers.InsertOnSubmit(newUser);
   db.SubmitChanges();

I want to get the actual sql command text that linq generated.

+1  A: 

db.Log is a TextWriter that you can use to get the text of the query.

db.Log = Console.Out
var newUser = new tblUser()
{ 
    Email = strEmail,
    Password = strPassword,
    DateBirth = DateTime.Parse(strDateBirth),
};
db.tblUsers.InsertOnSubmit(newUser);
db.SubmitChanges();

And it will write the query text to standard output.

Jason Punyon
It doesn't work for me. Why Console.Out? I'm using ASP.NET, it's a web app, I think I need response.write to output.I also tried to use Response.wirte but it prints nothing.
+1  A: 

Check this msdn article. You can use DataContext.Log property.

bruno conde
+3  A: 

You need to set the DataContext.Log property to a writer, you can wrap a writer around a stringbuilder then after your insert response.write your stringbuilder.tostring...

                        StringBuilder sb = new StringBuilder();
       StringWriter writer = new StringWriter(sb);
       Context.Log = writer;
       ...
        DOINSERT & SUBMITCHANGES
       ...
       Response.Write(sb.ToString());
Quintin Robinson