DB2 Version 10.1 for Linux, UNIX, and Windows

DB2CommandBuilder Class

Automatically generates single-table commands used to reconcile changes made to a DataSet with the associated database.

Namespace:
IBM®.Data.DB2
Assembly:
IBM.Data.DB2 (in IBM.Data.DB2.dll)

.NET Framework 2.0 3.0, 3.5, and 4.0 Inheritance hierarchy

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Data.Common.DbCommandBuilder
            IBM.Data.DB2.DB2CommandBuilder

.NET Framework 2.0 3.0, 3.5, and 4.0 Syntax

[Visual Basic]
NotInheritable Public Class DB2CommandBuilder
   Inherits DbCommandBuilder
[C#]
public sealed class DB2CommandBuilder : DbCommandBuilder
[C++]
public __gc __sealed class DB2CommandBuilder : public DbCommandBuilder
[JScript]
public final class DB2CommandBuilder extends DbCommandBuilder

Remarks

The DB2DataAdapter does not automatically generate the SQL statements required to reconcile changes made to a DataSet associated with the database. However, you can create an DB2CommandBuilder object that generates SQL statements for single-table updates by setting the SelectCommand property of the DB2DataAdapter. Then, the DB2CommandBuilder generates any additional SQL statements that you do not set.

The relationship between a DB2DataAdapter and its corresponding DB2CommandBuilder is always one-to-one. To create this correspondence, you set the DataAdapter property of the DB2CommandBuilder object. This registers the DB2CommandBuilder as a listener, which produces the output of DB2DataAdapter.RowUpdating events that affect the DataSet.

To generate INSERT, UPDATE, or DELETE statements, the DB2CommandBuilder uses the SelectCommand property, which retrieves a required set of metadata. If you change the value of SelectCommand after the metadata has been retrieved (for example, after the first update), you should call the RefreshSchema method to update the metadata.

As you cannot specify a value using the GENERATED ALWAYS AS column, this column will be skipped in the INSERT and SET clause of the UPDATE command.

Note: If the SELECT statement assigned to the SelectCommand property uses aliased column names, the resulting INSERT, UPDATE, and DELETE statements might be inaccurate or might fail. If the data server cannot provide the proper base column name for the alias column name, the alias name could be used in the generated INSERT, UPDATE, and DELETE statements. The generated INSERT, UPDATE, and DELETE statements would contain errors.

The DB2CommandBuilder also uses the DB2Command.Connection, DB2Command.CommandTimeout, and DB2Command.Transaction properties referenced by the SelectCommand. The user should call RefreshSchema if any of these properties are modified, or if the value of the SelectCommand property itself is changed. Otherwise, the DB2DataAdapter.InsertCommand, DB2DataAdapter.UpdateCommand, and DB2DataAdapter.DeleteCommand properties retain their previous values.

If you call Dispose, the DB2CommandBuilder is disassociated from the DB2DataAdapter, and the generated commands are no longer used.

The columns that cannot be used in the Where clause (blob, clob, xml) will be skipped from the where clause of the DB2DataAdapter.UpdateCommand, and DB2DataAdapter.DeleteCommand.

The columns that are returned as non-updatable in the DB2DataReader.GetSchemaTable method, are skipped in the DB2DataAdapter.InsertCommand and DB2DataAdapter.UpdateCommand method operations. The non-updatable columns are columns with one or more of the following metadata returned from the DB2DataReader.GetSchemaTable method:
  • IsAutoIncrement
  • IsReadOnly
  • IsRowVersion

Example

[Visual Basic, C#] The following example uses DB2Command, along with DB2®DataAdapter and DB2Connection, to select rows from a database. The example is passed an initialized DataSet, a connection string, a query string that is an SQL SELECT statement, and a string that is the name of the database table. The example then creates an DB2CommandBuilder.

[Visual Basic]
Public Function SelectDB2SrvRows(myDataSet As DataSet, myConnection As String, 
                mySelectQuery As String, myTableName As String) As DataSet
    Dim myConn As New DB2Connection(myConnection)
    Dim myDataAdapter As New DB2DataAdapter()
    myDataAdapter.SelectCommand = New DB2Command(mySelectQuery, myConn)
    Dim employeeCB As DB2CommandBuilder = New DB2CommandBuilder(myDataAdapter)

    myConn.Open()

    Dim employeeDS As DataSet = New DataSet()
    myDataAdapter.Fill(employeeDS, "EMPLOYEE")

    ' Code to modify data in DataSet here

    ' Without the DB2CommandBuilder this line would fail.
    myDataAdapter.Update(employeeDS, "EMPLOYEE")

    myConn.Close()

    Return employeeDS
End Function 'SelectDB2SrvRows
[C#]
public DataSet SelectDB2SrvRows(DataSet 
       myDataSet,string myConnection,string mySelectQuery,string myTableName)
{
    DB2Connection myConn = new DB2Connection(myConnection);
    DB2DataAdapter myDataAdapter = new DB2DataAdapter();
    myDataAdapter.SelectCommand = new DB2Command(mySelectQuery, myConn);
    DB2CommandBuilder employeeCB = new DB2CommandBuilder(myDataAdapter);

    myConn.Open();

    DataSet employeeDS = new DataSet();
    myDataAdapter.Fill(employeeDS, "EMPLOYEE");

    //code to modify data in dataset here

    //Without the DB2CommandBuilder this line would fail
    myDataAdapter.Update(employeeDS, "EMPLOYEE");

    myConn.Close();

    return employeeDS;
 }

Thread safety

Any public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread safe.

Version information

Last update
This topic was last updated for: IBM DB2 Version 9.7
.NET Framework version
Supported in: 2.0, 3.0, 3.5, and 4.0
IBM Data Server Client
Supported in: 8.1.2 and up