共用方式為


HOW TO:將資料集變更儲存至資料庫

更新:2007 年 11 月

修改和驗證資料集的資料之後,您或許想要將更新資料送回資料庫。若要將已修改的資料傳送至資料庫,請呼叫 TableAdapter (或資料配接器) 的 Update 方法。配接器的 Update 方法會更新單一資料表,並根據資料表中每個資料列的 RowState,執行正確命令 (INSERT、UPDATE 或 DELETE)。

當您儲存關聯資料表中的資料時,Visual Studio 2008 會提供新的 TableAdapterManager 元件,根據資料庫中所定義的外部索引鍵條件約束,協助以正確的順序執行儲存。如需詳細資訊,請參閱階層式更新概觀

注意事項:

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

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

  1. 執行 try/catch 區塊內可嘗試將更新送到資料庫的程式碼。

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

  3. 解決資料列中的問題 (如果可能的話就利用程式,或是將無效資料列提供給使用者進行修改),接著重新嘗試更新 (HasErrors 屬性、GetErrors 方法)。

將資料儲存至資料庫

呼叫 TableAdapter (或資料配接器) 的 Update 方法,將包含寫入值的資料表名稱傳遞至資料庫。如需將單一資料表的值存回資料庫的詳細資訊,請參閱逐步解說:儲存資料至資料庫 (單一資料表)

若要使用 TableAdapter 以資料集更新資料庫

  • 封入 try/catch 區塊內的 TableAdapter.Update 方法。下列範例將示範如何嘗試以 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");
    }
    

若要使用資料配接器,以資料集更新資料庫

  • 封入 try/catch 區塊內的 DataAdapter.Update 方法。下列範例將示範如何嘗試以 DataSet1 內的 Table1 內容來更新資料來源。

    Try
        SqlDataAdapter1.Update(Dataset1.Tables("Table1"))
    
    Catch x As Exception
        ' Error during Update, add code to locate error, reconcile 
        ' and try to update again.
    End Try
    
    try
    {
        SqlDataAdapter1.Update(Dataset1.Tables["Table1"]);
    }
    catch (Exception e)
    {
        // Error during Update, add code to locate error, reconcile 
        // and try to update again.
    }
    

使用資料集中的兩個關聯資料表

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

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

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

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

如需儲存多個資料表的資料之詳細資訊,請參閱逐步解說:儲存資料至資料庫 (多個資料表)

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

若要使用 TableAdapter 更新兩個關聯資料表

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

  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();
            }
        }
    }
    

若要使用資料配接器更新兩個關聯資料表

  • 呼叫每個資料配接器的 Update 方法。

    以下範例將顯示如何使用包含關聯資料表的資料集來更新資料來源。為了依照上述順序進行,範例中將建立三個暫存 DataTable 來存放不同的資料錄。然後會針對資料列的每個子集,從 try/catch 區塊中呼叫 Update 方法。如果發生更新錯誤,則會轉至建議的動作並解決問題。接著資料集會認可變更。最後,處置暫存資料表來釋出資源。

    Private Sub UpdateDB()
        Dim DeletedChildRecords As DataTable = _
            dsNorthwind1.Orders.GetChanges(DataRowState.Deleted)
    
        Dim NewChildRecords As DataTable = _
            dsNorthwind1.Orders.GetChanges(DataRowState.Added)
    
        Dim ModifiedChildRecords As DataTable = _
            dsNorthwind1.Orders.GetChanges(DataRowState.Modified)
    
        Try
            If Not DeletedChildRecords Is Nothing Then
                daOrders.Update(DeletedChildRecords)
            End If
    
            daCustomers.Update(dsNorthwind1, "Customers")
    
            If Not NewChildRecords Is Nothing Then
                daOrders.Update(NewChildRecords)
            End If
    
            If Not ModifiedChildRecords Is Nothing Then
                daOrders.Update(ModifiedChildRecords)
            End If
    
            dsNorthwind1.AcceptChanges()
    
        Catch ex As Exception
            ' Update error, resolve and try again
    
        Finally
            If Not DeletedChildRecords Is Nothing Then
                DeletedChildRecords.Dispose()
            End If
    
            If Not NewChildRecords Is Nothing Then
                NewChildRecords.Dispose()
            End If
    
            If Not ModifiedChildRecords Is Nothing Then
                ModifiedChildRecords.Dispose()
            End If
        End Try
    End Sub
    
    void UpdateDB()
    {
        System.Data.DataTable DeletedChildRecords = 
            dsNorthwind1.Orders.GetChanges(System.Data.DataRowState.Deleted);
    
        System.Data.DataTable NewChildRecords = 
            dsNorthwind1.Orders.GetChanges(System.Data.DataRowState.Added);
    
        System.Data.DataTable ModifiedChildRecords = 
            dsNorthwind1.Orders.GetChanges(System.Data.DataRowState.Modified);
    
        try
        {
            if (DeletedChildRecords != null)
            {
                daOrders.Update(DeletedChildRecords);
            }
            if (NewChildRecords != null)
            {
                daOrders.Update(NewChildRecords);
            }
            if (ModifiedChildRecords != null)
            {
                daOrders.Update(ModifiedChildRecords);
            }
    
            dsNorthwind1.AcceptChanges();
        }
    
        catch (Exception ex)
        {
            // Update error, resolve and try again
        }
    
        finally
        {
            if (DeletedChildRecords != null)
            {
                DeletedChildRecords.Dispose();
            }
            if (NewChildRecords != null)
            {
                NewChildRecords.Dispose();
            }
            if (ModifiedChildRecords != null)
            {
                ModifiedChildRecords.Dispose();
            }
        }
    }
    

請參閱

概念

資料的新功能

顯示資料概觀

其他資源

資料逐步解說

連接至 Visual Studio 中的資料

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

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

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

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

驗證資料

儲存資料