語言
Important
請勿在新的開發中使用 System.Data.SqlClient。 使用 Microsoft.Data.SqlClient 來開發新的 SQL Server 資料存取程式碼。 這些範例之所以保留System.Data.SqlClient,是因為 .NET Framework 內建的 LINQ to SQL 提供者需要這些。 僅僅改變連線類型並不會將內容遷移至 LINQ to SQL。
DataContext 是你連接資料庫、從中取回物件,並提交變更的主要管道。 你使用 DataContext 就像使用 ADO.NET SqlConnection 一樣。 事實上,DataContext 會使用你提供的 connection 或 連接字串 進行初始化。 欲了解更多資訊,請參閱 DataContext 方法(O/R 設計器)。
DataContext 介面的目的是將你對物件的請求轉換成對資料庫執行的 SQL 查詢,然後再根據結果組合物件。
DataContext 會透過實作與標準查詢運算子相同的運算子模式(例如 Where 和 Select),來啟用 Language-Integrated Query(LINQ)。
Important
維持安全連線是最重要的。 如需詳細資訊,請參閱 LINQ to SQL 中的安全性。
範例 1
在以下範例中,使用 該 DataContext 連接 Northwind 樣本資料庫,並檢索城市為倫敦的客戶列。
// Do not use System.Data.SqlClient for new development; use Microsoft.Data.SqlClient.
// LINQ to SQL's built-in provider requires System.Data.SqlClient.
// DataContext takes a connection string.
DataContext db = new DataContext(@"c:\Northwind.mdf");
// Get a typed table to run queries.
Table<Customer> Customers = db.GetTable<Customer>();
// Query for customers from London.
var query =
from cust in Customers
where cust.City == "London"
select cust;
foreach (var cust in query)
Console.WriteLine("id = {0}, City = {1}", cust.CustomerID, cust.City);
' Do not use System.Data.SqlClient for new development; use Microsoft.Data.SqlClient.
' LINQ to SQL's built-in provider requires System.Data.SqlClient.
' DataContext takes a connection string.
Dim db As New DataContext("…\Northwind.mdf")
' Get a typed table to run queries.
Dim Customers As Table(Of Customer) = db.GetTable(Of Customer)()
' Query for customer from London.
Dim Query = _
From cust In Customers _
Where cust.City = "London" _
Select cust
For Each cust In Query
Console.WriteLine("id=" & cust.CustomerID & _
", City=" & cust.City)
Next
每個資料庫資料表都表示為一個 Table 集合,可透過 GetTable 方法使用實體類別來識別。
範例 2
最佳實務是宣告強型別的 DataContext,而非依賴基本 DataContext 類別和 GetTable 方法。 強型別 DataContext 會宣告所有 Table 集合皆為上下文的成員,如下範例所示。
// Do not use System.Data.SqlClient for new development; use Microsoft.Data.SqlClient.
// LINQ to SQL's built-in provider requires System.Data.SqlClient.
public partial class Northwind : DataContext
{
public Table<Customer> Customers;
public Table<Order> Orders;
public Northwind(string connection) : base(connection) { }
}
' Do not use System.Data.SqlClient for new development; use Microsoft.Data.SqlClient.
' LINQ to SQL's built-in provider requires System.Data.SqlClient.
Partial Public Class Northwind
Inherits DataContext
Public Customers As Table(Of Customer)
Public Orders As Table(Of Order)
Public Sub New(ByVal connection As String)
MyBase.New(connection)
End Sub
End Class
你可以更簡單地將來自倫敦的客戶查詢表達為:
// Do not use System.Data.SqlClient for new development; use Microsoft.Data.SqlClient.
// LINQ to SQL's built-in provider requires System.Data.SqlClient.
Northwnd db = new Northwnd(@"c:\Northwnd.mdf");
var query =
from cust in db.Customers
where cust.City == "London"
select cust;
foreach (var cust in query)
Console.WriteLine("id = {0}, City = {1}", cust.CustomerID,
cust.City);
' Do not use System.Data.SqlClient for new development; use Microsoft.Data.SqlClient.
' LINQ to SQL's built-in provider requires System.Data.SqlClient.
Dim db As New Northwind("...\Northwnd.mdf")
Dim query = _
From cust In db.Customers _
Where cust.City = "London" _
Select cust
For Each cust In query
Console.WriteLine("id=" & cust.CustomerID & _
", City=" & cust.City)
Next