共用方式為


DataAdapter.AcceptChangesDuringUpdate 屬性

定義

取得或設定在 AcceptChanges() 期間是否呼叫 Update(DataSet)

public:
 property bool AcceptChangesDuringUpdate { bool get(); void set(bool value); };
public bool AcceptChangesDuringUpdate { get; set; }
member this.AcceptChangesDuringUpdate : bool with get, set
Public Property AcceptChangesDuringUpdate As Boolean

屬性值

如果在 AcceptChanges() 期間呼叫 Update(DataSet),則為 true,否則為 false。 預設為 true

範例

這則範例會示範如何從 DataTable 中擷取已變更的資料列,以及使用 SqlDataAdapter 來更新資料來源並擷取新的識別資料行值。 藉由將 AcceptChangesDuringUpdateSqlDataAdapter 屬性設定為 false 以保留原始的自動遞增值,新數據就可以合併到原始 DataTable,即使新的識別值不符合 中 DataTable原始的自動遞增值也一樣。

private static void MergeIdentityColumns(string connectionString)
{
    using (SqlConnection connection =
               new SqlConnection(connectionString))
    {
        // Create the DataAdapter
        SqlDataAdapter adapter =
            new SqlDataAdapter(
            "SELECT ShipperID, CompanyName FROM dbo.Shippers",
            connection);

        //Add the InsertCommand to retrieve new identity value.
        adapter.InsertCommand = new SqlCommand(
            "INSERT INTO dbo.Shippers (CompanyName) " +
            "VALUES (@CompanyName); " +
            "SELECT ShipperID, CompanyName FROM dbo.Shippers " +
            "WHERE ShipperID = SCOPE_IDENTITY();", connection);

        // Set AcceptChangesDuringUpdate to false
        adapter.AcceptChangesDuringUpdate = false;

        // Add the parameter for the inserted value.
        adapter.InsertCommand.Parameters.Add(
           new SqlParameter("@CompanyName", SqlDbType.NVarChar, 40,
           "CompanyName"));
        adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.Both;

        // MissingSchemaAction adds any missing schema to
        // the DataTable, including auto increment columns
        adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

        // Fill a DataTable.
        DataTable shipper = new DataTable();
        adapter.Fill(shipper);

        // Add a new shipper row.
        DataRow newRow = shipper.NewRow();
        newRow["CompanyName"] = "New Shipper";
        shipper.Rows.Add(newRow);

        // Add changed rows to a new DataTable. This
        // DataTable will be used to update the data source.
        DataTable dataChanges = shipper.GetChanges();

        adapter.Update(dataChanges);
        connection.Close();

        Console.WriteLine("Rows after merge.");
        foreach (DataRow rowBefore in shipper.Rows)
        {
            {
                Console.WriteLine("{0}: {1}", rowBefore[0], rowBefore[1]);
            }
        }

        // Merge the two DataTables to get new values.
        shipper.Merge(dataChanges);

        // Commit the changes.
        shipper.AcceptChanges();

        Console.WriteLine("Rows after merge.");
        foreach (DataRow rowAfter in shipper.Rows)
        {
            {
                Console.WriteLine("{0}: {1}", rowAfter[0], rowAfter[1]);
            }
        }
    }
}
Private Sub MergeIdentityColumns(ByVal connectionString As String)

    Using connection As SqlConnection = New SqlConnection( _
       connectionString)

        ' Create the DataAdapter
        Dim adapter As SqlDataAdapter = New SqlDataAdapter( _
          "SELECT ShipperID, CompanyName FROM dbo.Shippers", connection)

        ' Add the InsertCommand to retrieve new identity value.
        adapter.InsertCommand = New SqlCommand( _
            "INSERT INTO dbo.Shippers (CompanyName) " & _
            "VALUES (@CompanyName); " & _
            "SELECT ShipperID, CompanyName FROM dbo.Shippers " & _
            "WHERE ShipperID = SCOPE_IDENTITY();", _
            connection)

        ' Set AcceptChangesDuringUpdate to false.
        adapter.AcceptChangesDuringUpdate = False

        ' Add the parameter for the inserted value.
        adapter.InsertCommand.Parameters.Add( _
           New SqlParameter("@CompanyName", SqlDbType.NVarChar, 40, _
           "CompanyName"))
        adapter.InsertCommand.UpdatedRowSource = _
           UpdateRowSource.FirstReturnedRecord

        ' MissingSchemaAction adds any missing schema to 
        ' the DataTable, including auto increment columns
        adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey

        ' Fill a DataTable.
        Dim shipper As New DataTable
        adapter.Fill(shipper)

        ' Add a new shipper row. 
        Dim newRow As DataRow = shipper.NewRow()
        newRow("CompanyName") = "New Shipper"
        shipper.Rows.Add(newRow)

        ' Add changed rows to a new DataTable. This
        ' DataTable will be used to update the data source.
        Dim dataChanges As DataTable = shipper.GetChanges()

        ' Update the data source with the modified records.
        adapter.Update(dataChanges)

        Console.WriteLine("Rows before merge.")
        Dim rowBefore As DataRow
        For Each rowBefore In shipper.Rows
            Console.WriteLine("{0}: {1}", rowBefore(0), rowBefore(1))
        Next

        ' Merge the two DataTables to get new values.
        shipper.Merge(dataChanges)

        ' Commit the changes.
        shipper.AcceptChanges()

        Console.WriteLine("Rows after merge.")
        Dim rowAfter As DataRow
        For Each rowAfter In shipper.Rows
            Console.WriteLine("{0}: {1}", rowAfter(0), rowAfter(1))
        Next
    End Using
End Sub

備註

在呼叫 Update 的 方法DataAdapter期間,資料庫可以將數據傳回至您的 ADO.NET 應用程式作為輸出參數,或做為結果集的第一筆傳回記錄。 ADO.NET 可以擷取這些值並在正在更新的 DataRow 中更新對應的資料行。 根據預設,ADO.NET 在更新之後呼叫 AcceptChangesDataRow 方法。 不過,如果您想要將更新的數據列合併回另一個 DataTable,您可能會想要保留主鍵數據行的原始值。 例如,對應至資料庫中自動遞增數據行的主鍵數據行,例如識別數據行,可以包含資料庫指派的新值,這些值不符合 中所 DataRow指派的原始值。 根據預設,AcceptChanges會在更新之後隱含呼叫 ,而且數據列中可能已AutoIncrement由 ADO.NET 指派的值會遺失。 您可以將 屬性設定為 ,藉此防止ADO.NETAcceptChanges在 數據列上執行更新之後呼叫 ,藉此保留原始值,來保留 DataRow 中的原始值。falseAcceptChangesDuringUpdate

注意

AcceptChangesDuringUpdate 屬性設定為 會 false 套用至所有數據修改,而不只是插入。 如果您想要在相同的更新中編輯或刪除數據列,而且如果您想要只針對插入隱藏對的呼叫AcceptChanges,則不要將 設定falseAcceptChangesDuringUpdate為 ,而是針對 RowUpdated 的事件DataAdapter使用事件處理程式。 在事件處理程式中,您可以檢查 StatementType 以判斷資料修改是否為插入,如果 true為 ,請將 StatusRowUpdatedEventArgs 屬性設定為 SkipCurrentRow。 如需詳細資訊,請參閱擷取身分識別或自動編號值

適用於

另請參閱