藉由使用 LINQ to SQL,您可以使用 LINQ 技術來存取 SQL 資料庫,就像存取記憶體內部集合一樣。
例如,在下面的程式碼中,nw物件被創建來代表Northwind資料庫,Customers資料表是目標,將Customers從London中篩選出來,並選取用於擷取的CompanyName字串。
執行迴圈時,會擷取值的集合 CompanyName 。
// Northwnd inherits from System.Data.Linq.DataContext.
Northwnd nw = new Northwnd(@"northwnd.mdf");
// or, if you are not using SQL Server Express
// Northwnd nw = new Northwnd("Database=Northwind;Server=server_name;Integrated Security=SSPI");
var companyNameQuery =
from cust in nw.Customers
where cust.City == "London"
select cust.CompanyName;
foreach (var customer in companyNameQuery)
{
Console.WriteLine(customer);
}
' Northwnd inherits from System.Data.Linq.DataContext.
Dim nw As New Northwnd("c:\northwnd.mdf")
' or, if you are not using SQL Server Express
' Dim nw As New Northwnd("Database=Northwind;Server=dschwart7;Integrated Security=SSPI")
Dim companyNameQuery = _
From cust In nw.Customers _
Where cust.City = "London" _
Select cust.CompanyName
For Each customer In companyNameQuery
Console.WriteLine(customer)
Next
後續步驟
如需一些其他範例,包括插入和更新,請參閱 您可以使用LINQ to SQL 執行的動作。
接下來,請嘗試一些操作指南和教學課程,以實地體驗使用 LINQ to SQL。 請參閱 藉由逐步演示學習。
最後,閱讀 使用LINQ to SQL的一般步驟,瞭解如何開始使用您自己的LINQ to SQL專案。