当我使用Linq插入新记录时,如何获取SQL命令文本?

时间:2022-03-24 09:35:15
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 query that linq generated.

我想获得linq生成的实际SQL查询。

3 个解决方案

#1


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...

你需要将DataContext.Log属性设置为一个writer,你可以在一个stringbuilder周围包装一个writer然后插入response.write你的stringbuilder.tostring ...

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

#2


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

db.Log是一个TextWriter,可用于获取查询的文本。

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.

它会将查询文本写入标准输出。

#3


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

查看这篇msdn文章。您可以使用DataContext.Log属性。

#1


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...

你需要将DataContext.Log属性设置为一个writer,你可以在一个stringbuilder周围包装一个writer然后插入response.write你的stringbuilder.tostring ...

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

#2


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

db.Log是一个TextWriter,可用于获取查询的文本。

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.

它会将查询文本写入标准输出。

#3


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

查看这篇msdn文章。您可以使用DataContext.Log属性。