Language
Important
新しい開発には System.Data.SqlClient を使用しないでください。 新しい SQL Server データ アクセス コードには Microsoft.Data.SqlClient を使用します。 これらの例では、.NET Framework の組み込みの LINQ to SQL プロバイダーで必要になるため、System.Data.SqlClientが保持されます。 接続の種類だけを変更しても、LINQ to SQL は移行されません。
GetCommand を使用して、SQL コマンドやその他の情報を表示します。
例
次の例では、コンソール ウィンドウにクエリからの出力が表示され、その後に生成される SQL コマンド、コマンドの種類、接続の種類が表示されます。
// Do not use System.Data.SqlClient for new development; use Microsoft.Data.SqlClient.
// LINQ to SQL's built-in provider requires System.Data.SqlClient.
// 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 {z.ContactName}");
}
DbCommand dc = db.GetCommand(q);
Console.WriteLine($"\nCommand Text: \n{dc.CommandText}");
Console.WriteLine($"\nCommand Type: {dc.CommandType}");
Console.WriteLine($"\nConnection: {dc.Connection}");
Console.ReadLine();
' Do not use System.Data.SqlClient for new development; use Microsoft.Data.SqlClient.
' LINQ to SQL's built-in provider requires System.Data.SqlClient.
' 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()
出力は次のように表示されます。
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