來自: http://www.yaosansi.com/post/1380.html
記錄LINQ生成的SQL語句是常用的調試方式,而且能根據需要來優化LINQ生成的SQL語句,更能了深入的了解LINQ.
DataContext的Log屬性來將LINQ to SQL生成的SQL語句格式化.
一.控制臺程序(Console)
dataContext.Log = Console.Out;
二.利用GetCommand方法
dataContext.GetCommand(query).CommandText;
三.使用LINQPad ( 官方網站 )
LINQPad支持C# 3.0 和 Framework 3.5的全部功能:
- LINQ to SQL
- LINQ to Objects
- LINQ to XML
更多介紹請參考 李永京 的 學習LINQ工具:LINQPad
下載地址: http://www.albahari.com/LINQPad.exe
四.LINQ to SQL Debug Visualizer
ScottGu的LINQ to SQL Debug Visualizer可以在Debug過程中查看SQL語句.
介紹: http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx
下載: http://www.scottgu.com/blogposts/linqquery/SqlServerQueryVisualizer.zip
安裝方法
1. 關閉 VS2008。
2. 將壓縮包中的 SqlServerQueryVisualizer.dll 拷貝到 \Program Files\Microsoft Visual Studio 9.0\Common7\Packages\Debugger\Visualizers。
3. 重啟 VS2008 即可。
五.DebuggerWriter工具類
由于Console.Out方法在ASP.NET程序不起作用.
Kris Vandermotten 已經創建好了一個這個工具類, 你只要使用這樣的語法:
MyDataContext db =
new
MyDataContext();
db.Log =
new
DebuggerWriter();
?
asp.net可以選擇將Log信息直接發送到Debug的輸出窗口.
源碼:
using ?System;
using ?System.Diagnostics;
using ?System.Globalization;
using ?System.IO;
using ?System.Text;
![]()
namespace ?Vandermotten.Diagnostics
{
???? /// ? <summary>
???? /// ?Implements?a? <see?cref="TextWriter"/> ?for?writing?information?to?the?debugger?log.
???? /// ? </summary>
???? /// ? <seealso?cref="Debugger.Log"/>
???? public ? class ?DebuggerWriter?:?TextWriter
???? {
???????? private ? bool ?isOpen;
???????? private ? static ?UnicodeEncoding?encoding;
???????? private ? readonly ? int ?level;
???????? private ? readonly ? string ?category;
![]()
???????? /// ? <summary>
???????? /// ?Initializes?a?new?instance?of?the? <see?cref="DebuggerWriter"/> ?class.
???????? /// ? </summary>
???????? public ?DebuggerWriter()
????????????:? this ( 0 ,?Debugger.DefaultCategory)
???????? {
????????}
![]()
???????? /// ? <summary>
???????? /// ?Initializes?a?new?instance?of?the? <see?cref="DebuggerWriter"/> ?class?with?the?specified?level?and?category.
???????? /// ? </summary>
???????? /// ? <param?name="level"> A?description?of?the?importance?of?the?messages. </param>
???????? /// ? <param?name="category"> The?category?of?the?messages. </param>
???????? public ?DebuggerWriter( int ?level,? string ?category)
????????????:? this (level,?category,?CultureInfo.CurrentCulture)
???????? {
????????}
![]()
???????? /// ? <summary>
???????? /// ?Initializes?a?new?instance?of?the? <see?cref="DebuggerWriter"/> ?class?with?the?specified?level,?category?and?format?provider.
???????? /// ? </summary>
???????? /// ? <param?name="level"> A?description?of?the?importance?of?the?messages. </param>
???????? /// ? <param?name="category"> The?category?of?the?messages. </param>
???????? /// ? <param?name="formatProvider"> An? <see?cref="IFormatProvider"/> ?object?that?controls?formatting. </param>
???????? public ?DebuggerWriter( int ?level,? string ?category,?IFormatProvider?formatProvider)
????????????:? base (formatProvider)
???????? {
???????????? this .level? = ?level;
???????????? this .category? = ?category;
???????????? this .isOpen? = ? true ;
????????}
![]()
???????? protected ? override ? void ?Dispose( bool ?disposing)
???????? {
????????????isOpen? = ? false ;
???????????? base .Dispose(disposing);
????????}
![]()
???????? public ? override ? void ?Write( char ?value)
???????? {
???????????? if ?( ! isOpen)
???????????? {
???????????????? throw ? new ?ObjectDisposedException( null );
????????????}
????????????Debugger.Log(level,?category,?value.ToString());
????????}
![]()
???????? public ? override ? void ?Write( string ?value)
???????? {
???????????? if ?( ! isOpen)
???????????? {
???????????????? throw ? new ?ObjectDisposedException( null );
????????????}
???????????? if ?(value? != ? null )
???????????? {
????????????????Debugger.Log(level,?category,?value);
????????????}
????????}
![]()
???????? public ? override ? void ?Write( char []?buffer,? int ?index,? int ?count)
???????? {
???????????? if ?( ! isOpen)
???????????? {
???????????????? throw ? new ?ObjectDisposedException( null );
????????????}
???????????? if ?(buffer? == ? null ? || ?index? < ? 0 ? || ?count? < ? 0 ? || ?buffer.Length? - ?index? < ?count)
???????????? {
???????????????? base .Write(buffer,?index,?count);? // ?delegate?throw?exception?to?base?class
????????????}
????????????Debugger.Log(level,?category,? new ? string (buffer,?index,?count));
????????}
![]()
???????? public ? override ?Encoding?Encoding
???????? {
???????????? get
???????????? {
???????????????? if ?(encoding? == ? null )
???????????????? {
????????????????????encoding? = ? new ?UnicodeEncoding( false ,? false );
????????????????}
???????????????? return ?encoding;
????????????}
????????}
![]()
???????? public ? int ?Level
???????? {
???????????? get ? {? return ?level;?}
????????}
![]()
???????? public ? string ?Category
???????? {
???????????? get ? {? return ?category;?}
????????}
????}
}
![]()
六.將LINQ to SQL生成的SQL語句寫入日志文件
DataContext.Log是System.IO.TextWriter類型,所以你可以用以下的方法來做.
StreamWriter sw = new StreamWriter(
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "log.txt" ));
dBLinqDataContext.Log = sw;
var query = dataContext.Customers.Single<Customer>(c=>c.CustomerID.Contains( "s" ))
.Skip(0).Take(10).ToList();
sw.Flush();
sw.Close();
但以上方法有個缺點,就是需要在每個實現的方法中都寫這么多代碼.使用起來太不方便.參照dataContext.Log = Console.Out的表現形式
由是有了FileLog類.(當然,FileLog類除了此功能還有一些基本的記錄日志的方法)
使用時直接dataContext.Log = Yaosansi.IO.FileLog.Out;即可. 默認會在桌面上生成一個名叫UnNameFile.txt的文件.
當然如果你不想使用默認的文件名和路徑也可以使用dataContext.Log =new Yaosansi.IO.FileLog("FileName")的方式.
下面是FileLog類的源碼:
























更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
