亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

查看LINQ生成SQL語句的幾種方法

系統 2019 0

來自: 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類的源碼:

// 原文: http://www.yaosansi.com/post/1380.html
using ?System;
using ?System.Collections.Generic;
using ?System.Text;
using ?System.IO;

namespace ?Yaosansi.IO
{
????
/// ? <summary>
????
/// ?文件操作
????
/// ? </summary>

???? public ? class ?FileLog?:?TextWriter
????
{
????????
構造函數

????????
重寫TextWriter

????????
屬性

????????
方法


????}

}
http://www.cnblogs.com/songsh96/archive/2009/02/19/1393681.html

查看LINQ生成SQL語句的幾種方法


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产日韩欧美精品一区二区三区 | 中文字幕免费视频精品一 | a亚洲欧美中文日韩在线v日本 | 久青草国产手机视频免费观看 | 久久香焦 | 嫩模尺度私拍在线视频 | 97se综合 | 奇米777视频国产 | 十八女人毛片 | 精品日韩视频 | 日本欧美久久久久免费播放网 | 国产a一级毛片午夜剧场14 | 国产三级在线精品男人的天堂 | 欧美一区二区三区精品 | 丰满放荡岳乱妇91www | 欧美日韩免费 | 国语偷拍视频在线观看 | 4hu四虎永久网址 | 四虎国产精品永久在线 | 欧美成人禁片在线观看网址 | 国产精品视频久久 | 中文字幕一区久久久久 | 国产成人精品一区二三区在线观看 | 精品国产96亚洲一区二区三区 | 99精品视频99| 久久免费视频在线观看 | 一级毛片免费播放视频 | 欧美日本一区 | 免费国产免费福利视频 | 美女视频很黄很暴黄是免费的 | 欧美成人免费全部观看天天性色 | 国产不卡视频在线观看 | www.免费视频 | 亚洲精品欧美一区二区三区 | 成年女人免费看片 | 久草国产在线观看 | 欧美日韩顶级毛片www免费看 | 精品久久免费观看 | 日韩久久网 | 日本无翼乌全彩无遮挡动漫 | 九九这里只精品视在线99 |