共用方式為


逐步解說:儲存資料至資料庫 (多個資料表)

更新:2007 年 11 月

在應用程式的開發過程中,其中一個最常見的案例是在 Windows 應用程式的表單上顯示資料、編輯資料,以及將更新資料傳送回資料庫。這個逐步解說會建立表單,以便顯示來自兩個相關資料表的資料,並且示範如何編輯資料錄,以及將變更儲存回資料庫。這個範例會使用 Northwind 範例資料庫的 Customers 和 Orders 資料表。

您可以將應用程式的資料儲存回資料庫,只要呼叫 TableAdapter 的 Update 方法即可。當您從 [資料來源] 視窗拖曳項目時,會自動針對第一個放置到表單上的資料表加入要儲存資料的程式碼,任何加入到表單上的其他資料表則需要手動加入儲存資料所需的任何程式碼。此逐步解說將示範如何加入程式碼,以儲存多個資料表中的更新。

注意事項:

根據目前使用的設定與版本,您所看到的對話方塊與功能表命令可能會與 [說明] 中所描述的不同。若要變更設定,請從 [工具] 功能表中選擇 [匯入和匯出設定]。如需詳細資訊,請參閱 Visual Studio 設定

逐步解說將說明的工作包括:

必要條件

若要完成這個逐步解說,您必須要有:

建立 Windows 應用程式

第一個步驟是建立 [Windows 應用程式]。在這個步驟,指定專案名稱是選擇性的動作,但因為我們稍後要儲存它,所以會為它命名。

若要建立新的 Windows 應用程式專案

  1. 從 [檔案] 功能表中,建立新專案。

  2. 將專案命名為 UpdateMultipleTablesWalkthrough。

  3. 請選取 [Windows 應用程式],並按 [確定]。如需詳細資訊,請參閱建立 Windows 架構的應用程式

    隨即建立 UpdateMultipleTablesWalkthrough 專案,並將其加入至 [方案總管]。

建立資料來源

這個步驟使用 [資料來源組態精靈],從 Northwind 資料庫建立資料來源。您必須可以存取 Northwind 範例資料庫,才能建立連接。如需設定 Northwind 範例資料庫的詳細資訊,請參閱 HOW TO:安裝範例資料庫

若要建立資料來源

  1. 在 [資料] 功能表上,請按 [顯示資料來源]。

  2. 在 [資料來源] 視窗中,按一下 [加入新資料來源],啟動 [資料來源組態精靈]。

  3. 選取 [選擇資料來源類型] 頁面上的 [資料庫],再按 [下一步]。

  4. 在 [選擇資料連接] 頁面上,執行下列其中一項動作:

    • 如果下拉式清單中有提供 Northwind 範例資料庫的資料連接,請選取這個資料連接。

      -或-

    • 選取 [新增連接],開啟 [新增/修改連接] 對話方塊。如需詳細資訊,請參閱新增/修改連接對話方塊 (一般)

  5. 如果資料庫需要密碼,請選取選項來加入敏感性資料,然後按一下 [下一步]。

  6. 按一下 [將連接字串儲存到應用程式組態檔] 頁面上的 [下一步]。

  7. 在 [選擇您的資料庫物件] 頁面上,展開 [資料表] 節點。

  8. 選取 [Customers] 和 [Orders] 資料表,再按一下 [完成]。

    [NorthwindDataSet] 會加入專案中,且這些資料表會出現在 [資料來源] 視窗中。

設定要建立的控制項

在這個逐步解說中,Customers 資料表中的資料將設為 [Details] 配置,也就是在個別控制項中顯示資料。Orders 資料表中的資料將會設為 DataGridView 控制項內顯示的 [格線] 配置。

若要在資料來源視窗中設定項目的置放類型

  1. 在 [資料來源] 視窗中,展開 [Customers] 節點。

  2. 從 [Customers] 節點的控制項清單中選取 [Details],即可將 [Customers] 資料表的控制項變更為個別控制項。如需詳細資訊,請參閱 HOW TO:設定從資料來源視窗拖曳時要建立的控制項

建立資料繫結表單

從 [資料來源] 視窗將項目拖曳至表單上,即可建立資料繫結控制項。

若要在表單上建立資料繫結控制項

  1. 從 [資料來源] 視窗,將主要 [Customers] 節點拖曳至 [Form1]。

    具有描述性標籤的資料繫結控制項以及用於巡覽資料錄的工具區域 (BindingNavigator) 會出現在表單上。NorthwindDataSetCustomersTableAdapterBindingSourceBindingNavigator 則會出現在元件匣中。

  2. 從 [資料來源] 視窗,將相關的 [Orders] 節點拖曳至 [Form1]。

    注意事項:

    相關的 [Orders] 節點位於 [Fax] 資料行下方,是 [Customers] 節點的子節點。

    DataGridView 控制項以及用於巡覽資料錄的工具區域 (BindingNavigator) 會出現在表單上。OrdersTableAdapterBindingSource 則會出現在元件匣中。

加入程式碼以更新資料庫

您可以呼叫 [Customers] 和 [Orders] TableAdapter 的 Update 方法來更新資料庫。根據預設,BindingNavigator 的 [儲存] 按鈕之事件處理常式會加入至表單的程式碼,將更新傳送至資料庫。這個程序會修改此程式碼,依適當順序傳送更新,以排除引發參考完整性錯誤的可能性。這個程式碼也會在 try-catch 區塊中包裝更新呼叫,以實作錯誤處理。您可以修改程式碼,使其符合應用程式的需求。

注意事項:

為了簡明起見,這個逐步解說不使用交易,但是如果您要更新兩個或兩個以上的關聯資料表,則應該在交易中加入所有更新邏輯。交易處理序會確定所有資料庫相關變更都成功後,才會認可任何變更。如需詳細資訊,請參閱交易和並行 (ADO.NET)

若要將更新邏輯加入至應用程式

  1. 按兩下 BindingNavigator 上的 [儲存] 按鈕,在開啟的 [程式碼編輯器] 中顯示 bindingNavigatorSaveItem_Click 事件處理常式。

  2. 取代事件處理常式中的程式碼,以呼叫相關 TableAdapter 的 Update 方法。下列程式碼會先建立三個暫存資料表,以保存每個 DataRowState (DeletedAddedModified) 的更新資訊。然後依適當順序執行更新。程式碼外觀應為:

    Me.Validate()
    Me.OrdersBindingSource.EndEdit()
    Me.CustomersBindingSource.EndEdit()
    
    Dim deletedOrders As NorthwindDataSet.OrdersDataTable = CType( _
        NorthwindDataSet.Orders.GetChanges(Data.DataRowState.Deleted), NorthwindDataSet.OrdersDataTable)
    
    Dim newOrders As NorthwindDataSet.OrdersDataTable = CType( _
        NorthwindDataSet.Orders.GetChanges(Data.DataRowState.Added), NorthwindDataSet.OrdersDataTable)
    
    Dim modifiedOrders As NorthwindDataSet.OrdersDataTable = CType( _
        NorthwindDataSet.Orders.GetChanges(Data.DataRowState.Modified), NorthwindDataSet.OrdersDataTable)
    
    
    Try
        ' Remove all deleted orders from the Orders table.
        If Not deletedOrders Is Nothing Then
            OrdersTableAdapter.Update(deletedOrders)
        End If
    
        ' Update the Customers table.
        CustomersTableAdapter.Update(NorthwindDataSet.Customers)
    
        ' Add new orders to the Orders table.
        If Not newOrders Is Nothing Then
            OrdersTableAdapter.Update(newOrders)
        End If
    
        ' Update all modified Orders.
        If Not modifiedOrders Is Nothing Then
            OrdersTableAdapter.Update(modifiedOrders)
        End If
    
        NorthwindDataSet.AcceptChanges()
    
    Catch ex As Exception
        MsgBox("Update failed")
    
    Finally
        If Not deletedOrders Is Nothing Then
            deletedOrders.Dispose()
        End If
    
        If Not newOrders Is Nothing Then
            newOrders.Dispose()
        End If
    
        If Not modifiedOrders Is Nothing Then
            modifiedOrders.Dispose()
        End If
    End Try
    
    this.Validate();
    this.ordersBindingSource.EndEdit();
    this.customersBindingSource.EndEdit();
    
    NorthwindDataSet.OrdersDataTable deletedOrders = (NorthwindDataSet.OrdersDataTable)
        northwindDataSet.Orders.GetChanges(DataRowState.Deleted);
    
    NorthwindDataSet.OrdersDataTable newOrders = (NorthwindDataSet.OrdersDataTable)
        northwindDataSet.Orders.GetChanges(DataRowState.Added);
    
    NorthwindDataSet.OrdersDataTable modifiedOrders = (NorthwindDataSet.OrdersDataTable)
        northwindDataSet.Orders.GetChanges(DataRowState.Modified);
    
    try
    {
        // Remove all deleted orders from the Orders table.
        if (deletedOrders != null)
        {
            ordersTableAdapter.Update(deletedOrders);
        }
    
        // Update the Customers table.
        customersTableAdapter.Update(northwindDataSet.Customers);
    
        // Add new orders to the Orders table.
        if (newOrders != null)
        {
            ordersTableAdapter.Update(newOrders);
        }
    
        // Update all modified Orders.
        if (modifiedOrders != null)
        {
            ordersTableAdapter.Update(modifiedOrders);
        }
    
        northwindDataSet.AcceptChanges();
    }
    
    catch (System.Exception ex)
    {
        MessageBox.Show("Update failed");
    }
    
    finally
    {
        if (deletedOrders != null)
        {
            deletedOrders.Dispose();
        }
        if (newOrders != null)
        {
            newOrders.Dispose();
        }
        if (modifiedOrders != null)
        {
            modifiedOrders.Dispose();
        }
    }
    

測試應用程式

若要測試應用程式

  1. 按 F5。

  2. 在每個資料表中,變更一或多個資料錄的資料。

  3. 按下 [儲存] 按鈕。

  4. 檢查資料庫的值,確認變更已儲存。

後續步驟

根據應用程式的需求而定,您在建立資料繫結的 Windows 應用程式表單後可能還會想執行幾個步驟。您可以對這個逐步解說進行加強的部分包括:

請參閱

概念

資料的新功能

顯示資料概觀

其他資源

資料逐步解說

資料存取使用者入門

連接至 Visual Studio 中的資料

準備您的應用程式以接收資料

將資料擷取至您的應用程式中

顯示 Windows 應用程式之表單上的資料

在您的應用程式中編輯資料

驗證資料

儲存資料