日本語PDF

OracleCommandBuilderクラス

OracleCommandBuilderオブジェクトでは、データベースが更新される場合に、OracleDataAdapterに対して自動SQL生成が行われます。

クラスの継承

System.Object

  System.MarshalByRefObject

    System.ComponentModel.Component

      System.Data.Common.DbCommandBuilder

        OracleDataAccess.Client.OracleCommandBuilder

宣言

// C#
public sealed class OracleCommandBuilder : DbCommandBuilder

要件

プロバイダ ODP.NET管理対象外ドライバ ODP.NET管理対象ドライバ ODP.NET Core

アセンブリ

Oracle.DataAccess.dll

Oracle.ManagedDataAccess.dll

Oracle.ManagedDataAccess.dll

ネームスペース

Oracle.DataAccess.Client

Oracle.ManagedDataAccess.Client

Oracle.ManagedDataAccess.Client

.NET Framework

3.5, 4.5, 4.6, 4.7

4.5, 4.6, 4.7

4.6.1以降

.NET Core

-

-

2.1以降

スレッド安全性

パブリック静的メソッドはスレッドセーフですが、インスタンス・メソッドではスレッド安全性は保証されません。

備考

OracleCommandBuilderでは、OracleDataAdapterSelectCommandプロパティが設定されている場合、単一表の更新に対してSQL文が自動的に生成されます。DataSetに複数の表が含まれている場合、例外がスローされます。DataAdapterプロパティが設定されている場合は、OracleCommandBuilderRowUpdatingイベントのリスナーとして自己登録します。一度に関連付けることができるのは、1つのOracleDataAdapterオブジェクトと1つのOracleCommandBuilderオブジェクトのみです。

INSERTUPDATEまたはDELETE文を生成する場合、OracleCommandBuilderでは、必要なメタデータ・セットを取得するため、DataSet内のExtendedPropertiesが使用されます。メタデータの取得後にSelectCommandが変更された場合(最初の更新後など)、メタデータを更新するためRefreshSchemaメソッドがコールされる必要があります。

OracleCommandBuilderにより、最初にDataSetExtendedPropertiesのメタデータが検索されます。メタデータがない場合、OracleCommandBuilderではOracleDataAdapterSelectCommandプロパティによってメタデータが取得されます。

次の例では、EMP表の更新を実行しています。ここでは、OracleDataAdapter.Update()がコールされた場合のOracleDataAdapterオブジェクトに対するUpdateCommandを作成するために、OracleCommandBuilderオブジェクトが使用されています。

// C#
 
using System;
using System.Data;
using Oracle.DataAccess.Client;
 
class OracleCommandBuilderSample
{
  static void Main()
  {
    string constr = "User Id=scott;Password=tiger;Data Source=oracle";
    string cmdstr = "SELECT empno, sal from emp";
 
    // Create the adapter with the selectCommand txt and the
    // connection string
    OracleDataAdapter adapter = new OracleDataAdapter(cmdstr, constr);
 
    // Create the builder for the adapter to automatically generate
    // the Command when needed
    OracleCommandBuilder builder = new OracleCommandBuilder(adapter);
 
    // Create and fill the DataSet using the EMP
    DataSet dataset = new DataSet();
    adapter.Fill(dataset, "EMP");
 
    // Get the EMP table from the dataset
    DataTable table = dataset.Tables["EMP"];
 
    // Indicate DataColumn EMPNO is unique
    // This is required by the OracleCommandBuilder to update the EMP table
    table.Columns["EMPNO"].Unique = true;
 
    // Get the first row from the EMP table
    DataRow row = table.Rows[0];
 
    // Update the salary
    double sal = double.Parse(row["SAL"].ToString());
    row["SAL"] = sal + .01;
 
    // Now update the EMP using the adapter
    // The OracleCommandBuilder will create the UpdateCommand for the
    // adapter to update the EMP table
    adapter.Update(dataset, "EMP");
 
    Console.WriteLine("Row updated successfully");
  }
}