ADO.NET Provider for EnterpriseDB

Build 22.0.8479

バッチ処理

CData ADO.NET Provider for EnterpriseDB では、EnterpriseDBDataAdapters を通じて、EnterpriseDB の一括ロードサポートを活用することができます。Batch API を使用して、関連するSQL データ操作ステートメントを同時に実行できます。

ADO.NET Batch API の使用

バッチ更新を実行するには、次の基本的な手順を実行します。

  1. EnterpriseDBCommand オブジェクトで、パラメータ化されたカスタムSQL ステートメントを定義します。
  2. EnterpriseDBCommand オブジェクトのUpdatedRowSource プロパティを"UpdateRowSource.None" に設定します。
  3. EnterpriseDBCommand オブジェクトをEnterpriseDBDataAdapter に割り当てます。
  4. パラメータをコマンドに追加します。
  5. EnterpriseDBDataAdapter のUpdate メソッドを呼び出します。変更を含むDataSet またはDataTable を渡します。

バッチサイズの制御

要求のサイズ、ネットワークリソース、サーバーのパフォーマンスなどの要因に応じて、より小さなバッチ要求をいくつか実行することでパフォーマンスを向上させることができます。各バッチのサイズは、EnterpriseDBDataAdapter のUpdateBatchSize プロパティを正の整数に設定することで制御できます。

一括挿入

次のコードはレコードを一括で挿入する単一のバッチを準備します。。 この例では、"Added" 状態の新しいDataRows の一括挿入を実行します。

C#

EnterpriseDBDataAdapter adapter = new EnterpriseDBDataAdapter();

using (EnterpriseDBConnection conn = new EnterpriseDBConnection("User=postgres;Password=admin;Database=postgres;Server=127.0.0.1;Port=5444")) {
  conn.Open();
  adapter.InsertCommand = conn.CreateCommand();
  adapter.InsertCommand.CommandText = "INSERT INTO \"postgres\".\"public\".Orders (ShipCity) VALUES (@ShipCity)";
  adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None;
  adapter.InsertCommand.Parameters.Add("@ShipCity", "ShipCity");

  DataTable batchDataTable = new DataTable();
  batchDataTable.Columns.Add("ShipCity", typeof(string));
  batchDataTable.Rows.Add("Raleigh");
  batchDataTable.Rows.Add("New York");
  adapter.UpdateBatchSize = 2;
  adapter.Update(batchDataTable);
}

VB.NET

 
Dim adapter As New EnterpriseDBDataAdapter()

Using conn As New EnterpriseDBConnection("User=postgres;Password=admin;Database=postgres;Server=127.0.0.1;Port=5444")
  conn.Open()
  adapter.InsertCommand = conn.CreateCommand()
  adapter.InsertCommand.CommandText = "INSERT INTO \"postgres\".\"public\".Orders (ShipName) VALUES (@ShipCity)"
  adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None
  adapter.InsertCommand.Parameters.Add("@ShipCity", "ShipCity")

  Dim batchDataTable As New DataTable()
  batchDataTable.Columns.Add("ShipCity", GetType(String))
  batchDataTable.Rows.Add("New York")
  batchDataTable.Rows.Add("Raleigh")
  adapter.UpdateBatchSize = 2
  adapter.Update(batchDataTable)
End Using

一括更新

バッチ更新では、さらに、更新する各行の主キーが必要です。次の例では、"Modified" 状態のすべてのDataRow レコードに対してバッチを実行します。

C#

EnterpriseDBDataAdapter adapter = new EnterpriseDBDataAdapter();

using (EnterpriseDBConnection conn = new EnterpriseDBConnection("User=postgres;Password=admin;Database=postgres;Server=127.0.0.1;Port=5444")) { 
  conn.Open();
  adapter.UpdateCommand = conn.CreateCommand();
  adapter.UpdateCommand.CommandText = "UPDATE \"postgres\".\"public\".Orders SET ShipCity=@ShipCity WHERE Id=@Id";
  adapter.UpdateCommand.Parameters.Add("@ShipCity", "ShipCity");
  adapter.UpdateCommand.Parameters.Add("@Id", "Id");
  adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None; 
  adapter.UpdateBatchSize = 2;
  adapter.Update(dataTable);
}

VB.NET

 
Dim adapter As New EnterpriseDBDataAdapter()

Using conn As New EnterpriseDBConnection("User=postgres;Password=admin;Database=postgres;Server=127.0.0.1;Port=5444")
  conn.Open()
  adapter.UpdateCommand = conn.CreateCommand()
  adapter.UpdateCommand.CommandText = "UPDATE \"postgres\".\"public\".Orders SET ShipCity=@ShipCity WHERE Id=@Id"
  adapter.UpdateCommand.Parameters.Add("@ShipCity", "ShipCity")
  adapter.UpdateCommand.Parameters.Add("@Id", "Id")
  adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None
  adapter.UpdateBatchSize = 2
  adapter.Update(dataTable)
End Using

一括削除

次のコードは、レコードを一括で削除する単一のバッチを準備します。各行の主キーが必要です。次の例では、"Deleted" 状態のすべてのDataRow レコードに対してバッチを実行します。

C#

EnterpriseDBDataAdapter adapter = new EnterpriseDBDataAdapter();

using (EnterpriseDBConnection conn = new EnterpriseDBConnection("User=postgres;Password=admin;Database=postgres;Server=127.0.0.1;Port=5444")) {
  conn.Open();
  adapter.DeleteCommand = conn.CreateCommand();
  adapter.DeleteCommand.CommandText = "DELETE FROM \"postgres\".\"public\".Orders WHERE Id=@Id";
  adapter.DeleteCommand.Parameters.Add("@Id", "Id");
  adapter.DeleteCommand.UpdatedRowSource = UpdateRowSource.None; 
  adapter.UpdateBatchSize = 2;
  adpater.Update(table);
}

VB.NET

Dim adapter As New EnterpriseDBDataAdapter()

Using conn As New EnterpriseDBConnection("User=postgres;Password=admin;Database=postgres;Server=127.0.0.1;Port=5444")
  conn.Open()
  adapter.DeleteCommand = conn.CreateCommand()
  adapter.DeleteCommand.CommandText = "DELETE FROM \"postgres\".\"public\".Orders WHERE Id=@Id"
  adapter.DeleteCommand.Parameters.Add("@Id", "Id")
  adapter.DeleteCommand.UpdatedRowSource = UpdateRowSource.None 
  adapter.UpdateBatchSize = 2
  adpater.Update(table)
End Using

Copyright (c) 2023 CData Software, Inc. - All rights reserved.
Build 22.0.8479