開始使用Getting Started

藉由使用 LINQ to SQLLINQ to SQL ,您可以使用 LINQ 技術來存取 SQL 資料庫,就如同存取記憶體中的集合一樣。By using LINQ to SQLLINQ to SQL, you can use the LINQ technology to access SQL databases just as you would access an in-memory collection.

例如,下列程式碼會建立 nw 物件來表示 Northwind 資料庫、目標是設定為 Customers 資料表、篩選來自 CustomersLondon 的資料列,以及選取 CompanyName 的字串進行擷取。For example, the nw object in the following code is created to represent the Northwind database, the Customers table is targeted, the rows are filtered for Customers from London, and a string for CompanyName is selected for retrieval.

執行迴圈 (Loop) 時,會擷取 CompanyName 值的集合。When the loop is executed, the collection of CompanyName values is retrieved.

// 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

後續步驟Next Steps

如需其他範例,包括插入和更新,請參閱您可以如何使用 LINQ to SQLFor some additional examples, including inserting and updating, see What You Can Do With LINQ to SQL.

接下來,請嘗試一些逐步解說和教學課程,以獲取使用 LINQ to SQLLINQ to SQL 的實務經驗。Next, try some walkthroughs and tutorials to have a hands-on experience of using LINQ to SQLLINQ to SQL. 請參閱依逐步解說學習See Learning by Walkthroughs.

最後, LINQ to SQLLINQ to SQL 閱讀使用 LINQ to SQL 的一般步驟,瞭解如何開始著手您自己的專案。Finally, learn how to get started on your own LINQ to SQLLINQ to SQL project by reading Typical Steps for Using LINQ to SQL.

另請參閱See also