Language-Integrated 查詢 (LINQ) 查詢可讓您輕鬆地存取資料庫資訊,並修改資料庫中的值。
下列範例示範如何建立新的應用程式,以擷取和更新 SQL Server 資料庫中的資訊。
本文中的範例會使用 Northwind 範例資料庫。 若要取得資料庫,請參閱 下載範例資料庫。
建立連接到資料庫的連線
在 Visual Studio 中,單擊 [檢視] 功能表,然後選取 [/[資料庫總管],以開啟 [伺服器總管]/[資料庫總管]。
在 [伺服器總管]/中,以滑鼠右鍵按兩下 [數據連線],然後按兩下 [[新增連線]。
請指定 Northwind 範例資料庫的有效連線。
使用 LINQ to SQL 檔案新增專案
在 Visual Studio 的 [檔案] 功能表上,指向 [新增],然後按一下 [專案]。 選取 Visual Basic Windows Forms 應用程式 作為項目類型。
在 [專案] 功能表上,按一下 [新增專案]。 選取 LINQ to SQL 類別 項目範本。
將檔案命名為
northwind.dbml
。 點擊 新增。 對象關係型設計工具 (O/R 設計工具) 會針對northwind.dbml
檔案開啟。
若要在設計工具中新增資料表以進行查詢和修改
在 [伺服器總管]/[資料庫總管]中,展開與 Northwind 資料庫的連線。 展開 [數據表] 資料夾。
如果您已關閉 O/R 設計工具,您可以按兩下您稍早新增的
northwind.dbml
檔案來重新開啟它。按兩下 [客戶] 資料表,並將其拖曳至設計工具的左窗格。
設計師會為您的專案建立新的客戶物件。
儲存變更並關閉設計工具。
儲存您的專案。
若要新增程式代碼以修改資料庫並顯示結果
從 [工具箱],將 DataGridView 控件拖曳至專案 Form1 的預設 Windows Form。
當您將數據表新增至 O/R 設計工具時,設計工具會將 DataContext 物件新增至專案。 此物件包含可用來存取 Customers 資料表的程式代碼。 它也包含程式碼,定義了資料表中的本地 Customer 物件和 Customers 集合。 專案的 DataContext 物件會根據 .dbml 檔案的名稱來命名。 針對此項目,DataContext 物件會命名為
northwindDataContext
。您可以在程式代碼中建立 DataContext 對象的實例,並查詢和修改 O/R 設計工具所指定的 Customers 集合。 您對 Customers 集合所做的變更不會反映在資料庫中,直到您藉由呼叫 SubmitChanges 物件的 DataContext 方法提交這些變更為止。
按兩下 Windows Form1,將程式碼新增至 Load 事件,以查詢作為 DataContext屬性公開的 Customers 資料表。 新增下列程式代碼:
Private db As northwindDataContext Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs ) Handles MyBase.Load db = New northwindDataContext() RefreshData() End Sub Private Sub RefreshData() Dim customers = From cust In db.Customers Where cust.City(0) = "W" Select cust DataGridView1.DataSource = customers End Sub
從 [工具箱],將三個 Button 控件拖曳到窗體上。 選取第一個
Button
控件。 在 [屬性] 視窗中,將Name
控件的Button
設定為AddButton
,並將Text
設定為Add
。 選擇第二個按鈕,並將Name
屬性設定為UpdateButton
,並將Text
屬性設定為Update
。 選取第三個按鈕,並將Name
屬性設定為DeleteButton
,並將Text
屬性設定為Delete
。按兩下滑鼠左鍵 [新增] 按鈕,將程式代碼新增至其
Click
事件。 新增下列程式代碼:Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs ) Handles AddButton.Click Dim cust As New Customer With { .City = "Wellington", .CompanyName = "Blue Yonder Airlines", .ContactName = "Jill Frank", .Country = "New Zealand", .CustomerID = "JILLF"} db.Customers.InsertOnSubmit(cust) Try db.SubmitChanges() Catch ' Handle exception. End Try RefreshData() End Sub
按兩下 [Update] 按鈕,將程式代碼新增到其
Click
事件中。 新增下列程式代碼:Private Sub UpdateButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs ) Handles UpdateButton.Click Dim updateCust = (From cust In db.Customers Where cust.CustomerID = "JILLF").ToList()(0) updateCust.ContactName = "Jill Shrader" updateCust.Country = "Wales" updateCust.CompanyName = "Red Yonder Airlines" updateCust.City = "Cardiff" Try db.SubmitChanges() Catch ' Handle exception. End Try RefreshData() End Sub
按兩下 [刪除] 按鈕,將程式代碼新增至其
Click
事件中。 新增下列程式代碼:Private Sub DeleteButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs ) Handles DeleteButton.Click Dim deleteCust = (From cust In db.Customers Where cust.CustomerID = "JILLF").ToList()(0) db.Customers.DeleteOnSubmit(deleteCust) Try db.SubmitChanges() Catch ' Handle exception. End Try RefreshData() End Sub
按 F5 執行您的專案。 按一下 新增 以添加新記錄。 按一下 [更新] 來修改新記錄。 按一下 [刪除] 以刪除新記錄。