Share via


HOW TO:使用 TableAdapter 更新資料

更新:2011 年 4 月

修改和驗證資料集的資料之後,您或許想要將更新資料送回資料庫。 若要將已修改的資料傳送至資料庫,請呼叫 TableAdapter 的 Update 方法。 配接器的 Update 方法將更新單一資料表,並根據資料表中每個資料列的 RowState,執行正確命令 (INSERT、UPDATE 或 DELETE)。 當您將資料儲存在關聯資料表中時,Visual Studio 會提供新的 TableAdapterManager 元件,根據資料庫中所定義的外部索引鍵條件約束,協助以正確的順序執行儲存。 如需詳細資訊,請參閱 階層式更新概觀

注意事項注意事項

因為嘗試使用資料集的內容更新資料來源時可能造成錯誤,所以您應該將呼叫配接器之 Update 方法的程式碼,放置在 try/catch 區塊內。

更新資料來源的實際程序要視業務需求而定,不過您的應用程式應包含下列步驟:

  1. 呼叫 try/catch 區塊內之配接器的 Update 方法。

  2. 如果偵測到例外狀況,找出引發錯誤的資料列。 如需詳細資訊,請參閱 HOW TO:找尋有錯誤的資料列

  3. 解決資料列中的問題 (如果可以的話請以程式設計方式,或是將無效資料列提供給使用者修改),接著重新嘗試更新 (HasErrorsGetErrors)。

將資料儲存至資料庫

呼叫 TableAdapter 的 Update 方法,並將包含寫入值的資料表名稱傳遞給資料庫。

重要事項重要事項

當您使用本機資料庫 (例如 .mdf 檔案) 時,此檔案的 [複製到輸出目錄] 屬性不可設定為 [永遠複製]。 如果此屬性設定為 [永遠複製],建置專案時,此檔案會覆寫對本機資料庫所做的任何變更。 若要更正此問題,請在 [方案總管] 中以滑鼠右鍵按一下檔案,再按一下 [屬性],然後變更 [複製到輸出目錄] 的值。

若要使用 TableAdapter 更新內含資料集的資料庫

  • 將配接器的 Update 方法置於 try/catch 區塊內。 下列範例將示範如何從 try/catch 區塊內,使用 NorthwindDataSet 中的 Customers 資料表內容進行更新。

    Try
        Me.Validate()
        Me.CustomersBindingSource.EndEdit()
        Me.CustomersTableAdapter.Update(Me.NorthwindDataSet.Customers)
        MsgBox("Update successful")
    
    Catch ex As Exception
        MsgBox("Update failed")
    End Try
    
    try
    {
        this.Validate();
        this.customersBindingSource.EndEdit();
        this.customersTableAdapter.Update(this.northwindDataSet.Customers);
        MessageBox.Show("Update successful");
    }
    catch (System.Exception ex)
    {
        MessageBox.Show("Update failed");
    }
    

使用 TableAdapter 更新資料集的兩個關聯資料表

為了避免違反參考完整性條件約束,當您更新資料集中的關聯資料表時,必須以正確的順序進行更新。 命令執行的順序也會依照資料集中 DataRowCollection 的索引來進行。 若要避免引發資料完整性錯誤,最佳作法是依照下列順序更新資料庫:

  1. 子資料表:刪除資料錄。

  2. 父資料表:插入、更新及刪除資料錄。

  3. 子資料表:插入和更新資料錄。

    注意事項注意事項

    如果您要更新兩張以上關聯資料表,應該將所有更新邏輯包含在一次交易中。 交易程序會確定資料庫所有相關變更都成功後,才會認可任何變更。 如需詳細資訊,請參閱 交易和並行 (ADO.NET)

使用 TableAdapter 更新兩個相關資料表

  1. 建立三個暫存資料表來保存不同的資料錄。

  2. 從 try/catch 區塊呼叫每一個資料列子集的 Update 方法。 如果發生更新錯誤,您應該停止正在進行的動作並解決錯誤。

  3. 認可資料庫變更。

  4. 處置暫存資料表來釋出資源。

    以下範例將顯示如何使用包含關聯資料表的資料集來更新資料來源。

    Private Sub UpdateDB()
        Dim deletedChildRecords As NorthwindDataSet.OrdersDataTable =
            CType(NorthwindDataSet.Orders.GetChanges(Data.DataRowState.Deleted), NorthwindDataSet.OrdersDataTable)
    
        Dim newChildRecords As NorthwindDataSet.OrdersDataTable =
            CType(NorthwindDataSet.Orders.GetChanges(Data.DataRowState.Added), NorthwindDataSet.OrdersDataTable)
    
        Dim modifiedChildRecords As NorthwindDataSet.OrdersDataTable =
            CType(NorthwindDataSet.Orders.GetChanges(Data.DataRowState.Modified), NorthwindDataSet.OrdersDataTable)
    
        Try
            If deletedChildRecords IsNot Nothing Then
                OrdersTableAdapter.Update(deletedChildRecords)
            End If
    
            CustomersTableAdapter.Update(NorthwindDataSet.Customers)
    
            If newChildRecords IsNot Nothing Then
                OrdersTableAdapter.Update(newChildRecords)
            End If
    
            If modifiedChildRecords IsNot Nothing Then
                OrdersTableAdapter.Update(modifiedChildRecords)
            End If
    
            NorthwindDataSet.AcceptChanges()
    
        Catch ex As Exception
            MessageBox.Show("An error occurred during the update process")
            ' Add code to handle error here.
    
        Finally
            If deletedChildRecords IsNot Nothing Then
                deletedChildRecords.Dispose()
            End If
    
            If newChildRecords IsNot Nothing Then
                newChildRecords.Dispose()
            End If
    
            If modifiedChildRecords IsNot Nothing Then
                modifiedChildRecords.Dispose()
            End If
    
        End Try
    End Sub
    
    void UpdateDB()
    {
        NorthwindDataSet.OrdersDataTable deletedChildRecords = 
            (NorthwindDataSet.OrdersDataTable)northwindDataSet.Orders.GetChanges(DataRowState.Deleted);
    
        NorthwindDataSet.OrdersDataTable newChildRecords = 
            (NorthwindDataSet.OrdersDataTable)northwindDataSet.Orders.GetChanges(DataRowState.Added);
    
        NorthwindDataSet.OrdersDataTable modifiedChildRecords = 
            (NorthwindDataSet.OrdersDataTable)northwindDataSet.Orders.GetChanges(DataRowState.Modified);
    
        try
        {
            if (deletedChildRecords != null)
            {
                ordersTableAdapter.Update(deletedChildRecords);
            }
    
            customersTableAdapter.Update(northwindDataSet.Customers);
    
            if (newChildRecords != null)
            {
                ordersTableAdapter.Update(newChildRecords);
            }
    
            if (modifiedChildRecords != null)
            {
                ordersTableAdapter.Update(modifiedChildRecords);
            }
    
            northwindDataSet.AcceptChanges();
        }
    
        catch (Exception ex)
        {
            MessageBox.Show("An error occurred during the update process");
            // Add code to handle error here.
        }
    
        finally
        {
            if (deletedChildRecords != null)
            {
                deletedChildRecords.Dispose();
            }
            if (newChildRecords != null)
            {
                newChildRecords.Dispose();
            }
            if (modifiedChildRecords != null)
            {
                modifiedChildRecords.Dispose();
            }
        }
    }
    

請參閱

概念

TableAdapter 概觀

將 Windows Form 控制項繫結至 Visual Studio 中的資料

將控制項繫結至 Visual Studio 中的資料

其他資源

資料逐步解說

連接至 Visual Studio 中的資料

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

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

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

驗證資料

儲存資料

變更記錄

日期

記錄

原因

2011 年 4 月

解決本機資料庫檔案中 [複製到輸出目錄] 設定的可能問題。

客戶回函。