2011-04-10 54 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