dotConnect for DB2 Documentation
Devart.Data.DB2 Namespace / DB2CommandBuilder Class
Members Example

In This Topic
    DB2CommandBuilder Class
    In This Topic
    Automatically generates single-table commands used to reconcile changes made to a System.Data.DataSet with the DB2.
    Syntax
    'Declaration
     
    Public NotInheritable Class DB2CommandBuilder 
       Inherits Devart.Common.DbCommandBuilder
       Implements System.ComponentModel.IComponentSystem.IDisposable 
    Remarks

    The DB2DataAdapter does not automatically generate the SQL statements required to reconcile changes made to a System.Data.DataSet associated with DB2. However, you can create a DB2CommandBuilder object that generates SQL statements for single-table updates. After assigning the DB2DataAdapter to the DB2CommandBuilder, it begins to generate 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 causes the DB2CommandBuilder to register itself as a listener, which produces the output of DB2DataAdapter.RowUpdating events that affect the System.Data.DataSet.

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

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

    If you assign null to corresponding property of DB2DataAdapter, the DB2CommandBuilder will be disassociated from the DB2DataAdapter, and the generated commands will no longer be used.

    Note: This class is not available in .NET Standard 1.3 compatible assembly. It is available only in the assembly for full .NET Framework and .NET Standard 2.0 compatible assembly.

    Example
    The following example uses DB2Command, along with DB2DataAdapter and DB2Connection, to select rows from DB2. The example is passed an initialized System.Data.DataSet, a connection string, a query string that is SQL SELECT statement, and a string that is the name of the DB2 table. The example then creates a DB2CommandBuilder.
    public DataSet SelectDB2SrvRows(DataSet myDataSet,string db2Connection,string mySelectQuery,string myTableName)
    {
      DB2Connection myConn = new DB2Connection(db2Connection);
      DB2DataAdapter myDataAdapter = new DB2DataAdapter();
      myDataAdapter.SelectCommand = new DB2Command(mySelectQuery, myConn);
      DB2CommandBuilder db2CommandBuilder = new DB2CommandBuilder(myDataAdapter);
    
      myConn.Open();
    
      DataSet myDataSet = new DataSet();
      myDataAdapter.Fill(myDataSet, "Departments");
    
      //code to modify data in dataset here
    
      //Without the DB2CommandBuilder this line would fail
      myDataAdapter.Update(myDataSet, "Departments");
    
      myConn.Close();
    
      return myDataSet;
    }
    Public Function SelectDB2SrvRows(myDataSet As DataSet, db2Connection As String, mySelectQuery As String, myTableName As String) As DataSet
      Dim myConn As New DB2Connection(db2Connection)
      Dim myDataAdapter As New DB2DataAdapter()
      myDataAdapter.SelectCommand = New DB2Command(mySelectQuery, myConn)
      Dim db2CommandBuilder As DB2CommandBuilder = New DB2CommandBuilder(myDataAdapter)
    
      myConn.Open()
    
      Dim myDataSet As DataSet = New DataSet
      myDataAdapter.Fill(myDataSet, "Departments")
    
      ' Code to modify data in DataSet here
    
      ' Without the DB2CommandBuilder this line would fail.
      myDataAdapter.Update(myDataSet, "Departments")
    
      myConn.Close()
    
      SelectDB2SrvRows = myDataSet
    End Function
    Inheritance Hierarchy

    System.Object
       System.MarshalByRefObject
          System.ComponentModel.Component
             System.Data.Common.DbCommandBuilder
                Devart.Common.DbCommandBuilderBase
                   Devart.Common.DbCommandBuilder
                      Devart.Data.DB2.DB2CommandBuilder

    Requirements

    Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

    See Also