ADO.NET Provider for SingleStore

Build 23.0.8771

バッチ処理

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

ADO.NET Batch API の使用

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

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

バッチサイズの制御

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

一括INSERT

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

C#

SingleStoreDataAdapter adapter = new SingleStoreDataAdapter();

using (SingleStoreConnection conn = new SingleStoreConnection("User=myUser;Password=myPassword;Database=NorthWind;Server=myServer;Port=3306;")) {
  conn.Open();
  adapter.InsertCommand = conn.CreateCommand();
  adapter.InsertCommand.CommandText = "INSERT INTO `sakila`.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);

  SingleStoreCommand cmd = new SingleStoreCommand("SELECT * FROM LastResultInfo#TEMP", conn);
  adapter = new SingleStoreDataAdapter(cmd);
  DataTable res = new DataTable();
  adapter.Fill(res);
  foreach (DataRow row in res.Rows) 
    foreach(DataColumn col in res.Columns)
      Console.Write("{0}: {1}", col.ColumnName, row[col]);
}

VB.NET

 
Dim adapter As New SingleStoreDataAdapter()

Using conn As New SingleStoreConnection("User=myUser;Password=myPassword;Database=NorthWind;Server=myServer;Port=3306;")
  conn.Open()
  adapter.InsertCommand = conn.CreateCommand()
  adapter.InsertCommand.CommandText = "INSERT INTO `sakila`.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)

  Dim cmd As New SingleStoreCommand("SELECT * FROM LastResultInfo#TEMP", conn)
  adapter = New SingleStoreDataAdapter(cmd)
  Dim res As New DataTable()
  adapter.Fill(res)
  For Each row As DataRow In res.Rows 
    For Each col As DataColumn In res.Columns
      Console.WriteLine("{0}: {1}", col.ColumnName, row(col))
    Next
  Next
End Using

一括更新

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

C#

SingleStoreDataAdapter adapter = new SingleStoreDataAdapter();

using (SingleStoreConnection conn = new SingleStoreConnection("User=myUser;Password=myPassword;Database=NorthWind;Server=myServer;Port=3306;")) { 
  conn.Open();
  adapter.UpdateCommand = conn.CreateCommand();
  adapter.UpdateCommand.CommandText = "UPDATE `sakila`.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 SingleStoreDataAdapter()

Using conn As New SingleStoreConnection("User=myUser;Password=myPassword;Database=NorthWind;Server=myServer;Port=3306;")
  conn.Open()
  adapter.UpdateCommand = conn.CreateCommand()
  adapter.UpdateCommand.CommandText = "UPDATE `sakila`.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#

SingleStoreDataAdapter adapter = new SingleStoreDataAdapter();

using (SingleStoreConnection conn = new SingleStoreConnection("User=myUser;Password=myPassword;Database=NorthWind;Server=myServer;Port=3306;")) {
  conn.Open();
  adapter.DeleteCommand = conn.CreateCommand();
  adapter.DeleteCommand.CommandText = "DELETE FROM `sakila`.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 SingleStoreDataAdapter()

Using conn As New SingleStoreConnection("User=myUser;Password=myPassword;Database=NorthWind;Server=myServer;Port=3306;")
  conn.Open()
  adapter.DeleteCommand = conn.CreateCommand()
  adapter.DeleteCommand.CommandText = "DELETE FROM `sakila`.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) 2024 CData Software, Inc. - All rights reserved.
Build 23.0.8771