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를 사용하여 데이터 원본을 업데이트하고 새 ID 열 값을 검색하는 방법을 보여 줍니다. 의 속성을 SqlDataAdapterfalse 로 설정 AcceptChangesDuringUpdate 하여 원래 자동 증가 값을 유지하면 새 ID 값이 의 원래 자동 증가 값과 일치하지 않더라도 새 데이터를 원래 DataTableDataTable병합할 수 있습니다.

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 업데이트 후 의 DataRow 메서드를 호출 AcceptChanges 합니다. 그러나 업데이트된 행을 다른 DataTable에 다시 병합하려는 경우 기본 키 열의 원래 값을 유지하려고 할 수 있습니다. 예를 들어 ID 열과 같이 데이터베이스의 자동으로 증분 열에 해당하는 기본 키 열에는 에 할당된 원래 값과 일치하지 않는 데이터베이스에서 DataRow할당한 새 값이 포함될 수 있습니다. 기본적으로 는 AcceptChanges 업데이트 후에 암시적으로 호출되고 행의 원래 값(ADO.NET 할당된 값일 AutoIncrement 수 있음)이 손실됩니다. 원래 값을 DataRow 유지 하는 ADO.NET 로 속성을 false설정 AcceptChangesDuringUpdate 하여 행에 대한 업데이트를 수행 한 후 호출 AcceptChanges 할 수 없도록 하여 에서 원래 값을 유지할 수 있습니다.

참고

속성을 로 AcceptChangesDuringUpdatefalse 설정하면 삽입뿐만 아니라 모든 데이터 수정에 적용됩니다. 동일한 업데이트에서 행을 편집하거나 삭제하려는 경우 삽입에 대해서만 에 대한 호출 AcceptChanges 을 표시하지 않으려면 을 false로 설정하는 AcceptChangesDuringUpdate 대신 의 DataAdapter이벤트에 대한 RowUpdated 이벤트 처리기를 사용합니다. 이벤트 처리기에서 를 검사 StatementType 데이터 수정이 삽입인지 확인하고 이면 trueRowUpdatedEventArgsSkipCurrentRow속성을 로 설정할 Status 수 있습니다. 자세한 내용과 예는 ID 또는 자동 번호 값 검색을 참조하세요.

적용 대상

추가 정보