如何:使用 LINQ 修改資料庫中的資料 (Visual Basic)

Language-Integrated Query (LINQ) 查詢可讓您輕鬆地存取資料庫資訊,並修改資料庫中的值。

下列範例示範如何建立新的應用程式來擷取與更新 SQL Server 資料庫中的資訊。

本主題中的範例會使用 Northwind 範例資料庫。 如果您的開發電腦上沒有這個資料庫,則可以從 Microsoft 下載中心下載。 如需相關指示,請參閱下載範例資料庫

建立資料庫的連線

  1. 在 Visual Studio 中,按一下 [檢視] 功能表,然後選取 [伺服器總管] / [資料庫總管],以開啟 [伺服器總管] / [資料庫總管]。

  2. 在 [伺服器總管] / [資料庫總管] 中,以滑鼠右鍵按一下 [資料連線],然後按一下 [新增連線]。

  3. 指定 Northwind 範例資料庫的有效連線。

使用 LINQ to SQL 檔案新增專案

  1. 在 Visual Studio 的 [檔案] 功能表上,指向 [新增],然後按一下 [專案]。 將 Visual Basic 的 [Windows Forms 應用程式] 選為專案類型。

  2. 在 [專案] 功能表上,按一下 [加入新項目]。 選取 [LINQ to SQL 類別] 項目範本。

  3. 將檔案命名為 northwind.dbml。 按一下新增。 物件關聯式設計工具 (O/R 設計工具) 已為檔案 northwind.dbml 開啟。

若要新增表格以查詢和修改設計工具

  1. 在 [伺服器總管] / [資料庫總管] 中,展開與 Northwind 資料庫的連線。 展開 [資料表] 資料夾。

    如果您已關閉 O/R 設計工具,您可以按兩下稍早新增的 northwind.dbml 檔案來重新開啟。

  2. 按一下 [客戶] 表格,並拖曳至設計工具的左窗格。

    設計工具會為專案建立新的 Customer 物件。

  3. 儲存變更並關閉設計工具。

  4. 儲存您的專案。

若要新增程式碼以修改資料庫並顯示結果

  1. 從 [工具箱] 中,將 DataGridView 控制項拖曳至專案的預設 Windows Form (Form1)。

  2. 將表格新增至 O/R 設計工具時,設計工具會將 DataContext 物件新增至專案。 此物件包含可用來存取 Customers 表格的程式碼。 它也包含可為表格定義本機 Customer 物件和 Customers 集合的程式碼。 專案的 DataContext 物件會依據 .dbml 檔案名稱來命名。 此專案的 DataContext 物件會命名為 northwindDataContext

    您可以在程式碼中建立 DataContext 物件的執行個體,並查詢與修改 O/R 設計工具所指定的 Customers 集合。 在您呼叫 DataContext 物件的 SubmitChanges 方法來提交您對 Customers 集合所做的變更之前,這些變更都不會反映在資料庫中。

    按兩下 Windows Form (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
    
  3. 從 [工具箱] 將三個 控制項拖曳到表單上。Button 選取第一個 Button 控制項。 在 [屬性] 視窗中,將 Button 控制項的 Name 設為 AddButton,將 Text 設為 Add 選取第二個按鈕, 並將 Name 屬性設為 UpdateButton,將 Text 屬性設為 Update。 選取第三個按鈕, 並將 Name 屬性設為 DeleteButton ,將 Text 屬性設為 Delete

  4. 按兩下 [新增] 按鈕,將程式碼新增至其 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
    
  5. 按兩下 [更新] 按鈕,將程式碼新增至其 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
    
  6. 按兩下 [刪除] 按鈕,將程式碼新增至其 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
    
  7. 請按 F5 執行您的專案。 按一下 [新增] 以新增新記錄。 按一下 [更新] 以修改新記錄。 按一下 [刪除] 以刪除新記錄。

另請參閱