DataAdapter.AcceptChangesDuringUpdate プロパティ
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
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 列値を取得します。 元の自動インクリメント値を保持するようにプロパティを設定AcceptChangesDuringUpdate
することで、新しい ID 値が元のSqlDataAdapter自動インクリメント値と一致しない場合でも、新しいデータを元DataTableのDataTable
データにマージ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 値の取得」をご覧ください。