2011-04-10 59 views
0

使用Visual Studio 2010,.NET 4作为控制台应用程序。Linq to SQL Not Logging?

在using语句结尾处有一个断点时,Visual Studio将鼠标悬停在“stuff”上时显示select语句。但是,“stmt1”和“stmt2”都是空的。为什么?

谢谢。

控制台应用程序:

using (var cona = new mydbcontext(connstring)) 
    { 
     var stuff = from a in cona.testtable select a; 
     var stmt1 = cona.SqlStatement; 
     string stmt2 = cona.GetLoggedInformation(); 
    } 

public partial class mydbcontext : DataContext 
{ 
    private StringBuilder bldr = new StringBuilder(); 
    partial void OnCreated() 
    { 
     this.CommandTimeout = 120; 
     this.Log = new StringWriter(bldr); 
    } 
    public String GetLoggedInformation() 
    { 
     return bldr.ToString(); 
    } 
    public string SqlStatement 
    { 
     get { return this.bldr.ToString(); } 
    } 
} 

回答

1

最有可能的日志直到执行该语句写入。

试试这个

using (var cona = new mydbcontext(connstring)) 
{ 
    var stuff = from a in cona.testtable select a; 
    var data = stuff.ToList(); 
    var stmt1 = cona.SqlStatement; 
    string stmt2 = cona.GetLoggedInformation(); 
} 
+0

就是这样。我没有足够的耐心。一旦我有“stuff.ToList()”stmt1填充。谢谢! – Snowy 2011-04-10 15:27:20

+0

我很好奇,为什么在我原来的代码中,悬停在东西上显示了sql语句;这就像Visual Studio知道跟踪/字符串编写器不知道的东西。 – Snowy 2011-04-10 19:05:20