共用方式為


HOW TO:顯示 LINQ to SQL 命令 (LINQ to SQL)

使用 GetCommand,可以顯示 SQL 命令和其他資訊。

範例

在下列範例中,主控台視窗會顯示查詢的輸出,後面依序接著產生的 SQL 命令、命令的型別,以及連接的型別。

' Imports System.Data.Common
Dim db As New Northwnd("c:\northwnd.mdf")

Dim q = _
From cust In db.Customers _
Where cust.City = "London" _
Select cust

Console.WriteLine("Customers from London:")
For Each z As Customer In q
    Console.WriteLine(vbTab & z.ContactName)
Next

Dim dc As DbCommand = db.GetCommand(q)
Console.WriteLine(vbNewLine & "Command Text: " & vbNewLine & dc.CommandText)
Console.WriteLine(vbNewLine & "Command Type: {0}", dc.CommandType)
Console.WriteLine(vbNewLine & "Connection: {0}", dc.Connection)

Console.ReadLine()
// using System.Data.Common;
Northwnd db = new Northwnd(@"c:\northwnd.mdf");

var q =
    from cust in db.Customers
    where cust.City == "London"
    select cust;

Console.WriteLine("Customers from London:");
foreach (var z in q)
{
    Console.WriteLine("\t {0}",z.ContactName);
}

DbCommand dc = db.GetCommand(q);
Console.WriteLine("\nCommand Text: \n{0}",dc.CommandText);
Console.WriteLine("\nCommand Type: {0}",dc.CommandType);
Console.WriteLine("\nConnection: {0}",dc.Connection);

Console.ReadLine();

顯示的輸出如下:

    Customers from London:
        Thomas Hardy
        Victoria Ashworth
        Elizabeth Brown
        Ann Devon
        Simon Crowther
        Marie Bertrand
        Hari Kumar
        Dominique Perrier
    Command Text:
    SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactT
    itle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Coun
    try], [t0].[Phone], [t0].[Fax]
    FROM [dbo].[Customers] AS [t0]
    WHERE [t0].[City] = @p0
     
    Command Type: Text
     
    Connection: System.Data.SqlClient.SqlConnection

請參閱

其他資源

偵錯支援 (LINQ to SQL)