How to get SQL command text when inserting a new record in Linq?

            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 generated by linq.

+5
source share
3 answers

You need to set the DataContext.Log property for the writer, you can wrap the writer around the line editor after pasting response.write into your stringbuilder.tostring ...

                        StringBuilder sb = new StringBuilder();
                StringWriter writer = new StringWriter(sb);
                Context.Log = writer;
                ...
                    DOINSERT & SUBMITCHANGES
                ...
                Response.Write(sb.ToString());
+5
source

db.Log is a TextWriter that can be used to retrieve query text.

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

And he will write the request text to standard output.

+1
source

Mark the msdn article . You can use the property DataContext.Log.

+1
source

All Articles