共用方式為


快速入門

藉由使用 LINQ to SQL,您便能利用 LINQ 技術存取 SQL 資料庫,如同存取記憶體內部集合一樣。

例如,下列程式碼會建立 nw 物件來表示 Northwind 資料庫、目標是設定為 Customers 資料表、篩選來自 CustomersLondon 的資料列,以及選取 CompanyName 的字串進行擷取。

執行迴圈 (Loop) 時,會擷取 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 專案。

另請參閱