通过


如何:显示生成的 SQL

可以使用该 Log 属性查看为查询和更改处理生成的 SQL 代码。 此方法可用于了解 LINQ to SQL 功能以及调试特定问题。

示例

以下示例使用 Log 属性在执行代码之前在控制台窗口中显示 SQL 代码。 可以将此属性用于查询、插入、更新和删除命令。

控制台窗口中的行是在执行以下 Visual Basic 或 C# 代码时看到的。

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  
-- @p0: Input String (Size = 6; Prec = 0; Scale = 0) [London]  
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.20810.0  
AROUT  
BSBEV  
CONSH  
EASTC  
NORTS  
SEVES  
db.Log = Console.Out;
IQueryable<Customer> custQuery =
    from cust in db.Customers
    where cust.City == "London"
    select cust;

foreach(Customer custObj in custQuery)
{
    Console.WriteLine(custObj.CustomerID);
}
db.Log = Console.Out
Dim custQuery = _
    From cust In db.Customers _
    Where cust.City = "London" _
    Select cust

For Each custObj In custQuery
    Console.WriteLine(custObj.CustomerID)
Next

另见