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

プロパティ値

Boolean

AcceptChanges()Update(DataSet) が呼び出される場合は true。それ以外の場合は false。 既定値は、true です。

次の例では、DataTable から変更行を抽出し、SqlDataAdapter でデータ ソースを更新して、新しい ID 列値を取得します。 元の自動インクリメント値を保持するようにプロパティを設定AcceptChangesDuringUpdateすることで、新しい ID 値が元のSqlDataAdapter自動インクリメント値と一致しない場合でも、新しいデータを元DataTableDataTableデータにマージfalseできます。

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 アプリケーションにデータを返すことができます。 これらの値を取得することで、更新対象となる DataRow 内の対応する列を更新できます。 既定では、ADO.NET は更新後のメソッドをDataRow呼び出しますAcceptChanges。 ただし、更新された行を別 DataTableの行にマージする場合は、主キー列の元の値を保持することができます。 たとえば、ID 列など、データベース内の自動的にインクリメントされる列に対応する主キー列には、割り当てられた元の値と一致しないデータベースによって割り当てられた新しい値を DataRow含めることができます。 既定では、 AcceptChanges 更新後に暗黙的に呼び出され、ADO.NET によって割り当てられた可能性がある AutoIncrement 行の元の値は失われます。 元の値を保持するには、元のDataRow値を保持するADO.NETプロパティfalseを設定AcceptChangesDuringUpdateすることで、行に対して更新を実行した後に呼び出AcceptChangesさないようにします。

注意

AcceptChangesDuringUpdate挿入だけでなく、すべてのデータ変更に適用されるようにプロパティfalseを設定します。 同じ更新で行を編集または削除する場合、および挿入に対してのみ呼び出しをAcceptChanges抑制する場合は、設定AcceptChangesDuringUpdate``falseの代わりに、イベントハンドラーをRowUpdated``DataAdapter使用します。 イベント ハンドラーでは、データの変更が StatementType 挿入であるかどうかを確認し、その場合 trueは 、次のプロパティを Status RowUpdatedEventArgs SkipCurrentRow設定できます。 詳細と例については、「ID 値および Autonumber 値の取得」をご覧ください。

適用対象

こちらもご覧ください