Aracılığıyla paylaş


DataAdapter.AcceptChangesDuringUpdate Özellik

Tanım

bir sırasında Update(DataSet)çağrılıp çağrılmayacağını AcceptChanges() alır veya ayarlar.

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

Özellik Değeri

true sırasında çağrılırsa AcceptChanges()Update(DataSet); aksi takdirde false. Varsayılan değer: true.

Örnekler

Bu örnek, değiştirilen satırları DataTable içinden ayıklayıp SqlDataAdapter kullanarak veri kaynağını güncellemeyi ve yeni bir kimlik sütunu değeri almayı gösterir. özgün otomatik artış değerini korumak için false özelliğini olarak ayarlayarakAcceptChangesDuringUpdate, yeni kimlik değeri içindeki özgün otomatik artış değeriyle eşleşmese bile yeni veriler özgün DataTableile DataTablebirleştirilebilirSqlDataAdapter.

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

Açıklamalar

yöntemine UpdateDataAdapteryapılan çağrı sırasında veritabanı, çıktı parametreleri olarak veya sonuç kümesinin ilk döndürülen kaydı olarak verileri ADO.NET uygulamanıza geri gönderebilir. ADO.NET bu değerleri alabilir ve güncelleştirilmekte olan ilgili sütunları DataRow güncelleştirebilir. varsayılan olarak, ADO.NET güncelleştirmeden AcceptChanges sonra yöntemini DataRow çağırır. Ancak, güncelleştirilmiş satırı başka bir DataTableile birleştirmek istiyorsanız, birincil anahtar sütununun özgün değerini korumak isteyebilirsiniz. Örneğin, veritabanındaki kimlik sütunu gibi otomatik olarak artan bir sütuna karşılık gelen birincil anahtar sütunu, veritabanı tarafından atanan ve içinde DataRowatanan özgün değerlerle eşleşmeyen yeni değerler içerebilir. Varsayılan olarak, AcceptChanges bir güncelleştirmeden sonra örtük olarak çağrılır ve ADO.NET AutoIncrement tarafından atanmış olabilecek satırdaki özgün değerler kaybolur. özgün değerleri DataRow korumak için özelliğini falseolarak ayarlayarakAcceptChangesDuringUpdate, bir satırda güncelleştirme gerçekleştirdikten sonra çağrısı AcceptChanges yapmasını engelleyerek ADO.NET içindeki özgün değerleri koruyabilirsiniz.

Uyarı

özelliğini false olarak AcceptChangesDuringUpdate ayarlamak, yalnızca eklemeler için değil, tüm veri değişikliklerine de uygulanır. Aynı güncelleştirmedeki satırları düzenlemek veya silmek istiyorsanız ve yalnızca eklemeler için çağrısının AcceptChanges gösterilmemesini istiyorsanız, olarak ayarlamak falseAcceptChangesDuringUpdate yerine , olayı DataAdapteriçin RowUpdated bir olay işleyicisi kullanın. Olay işleyicisinde, veri değişikliğinin StatementType bir ekleme olup olmadığını belirlemek için öğesini denetleyebilirsiniz ve ise trueöğesinin RowUpdatedEventArgs özelliğini olarak SkipCurrentRowayarlayabilirsinizStatus. Daha fazla bilgi ve bir örnek için bkz. Kimlik veya Otomatik Sayı Değerlerini Alma işlemi.

Şunlara uygulanır

Ayrıca bkz.