dotConnect Universal Documentation
Devart.Data.Universal Namespace / UniCommandBuilder Class
Members Example

UniCommandBuilder Class
Allows to generate automatically single-table commands used to reconcile changes made to a System.Data.DataSet with the associated database.
Syntax
'Declaration
 
Public NotInheritable Class UniCommandBuilder 
   Inherits Devart.Common.DbCommandBuilder
   Implements System.ComponentModel.IComponentSystem.IDisposable 
 
Remarks
The UniDataAdapter does not automatically generate the SQL statements required to reconcile changes made to a System.Data.DataSet associated with the database. However, you can create a UniCommandBuilder object that generates SQL statements for single-table updates. After assigning the UniDataAdapter to the UniCommandBuilder, it begins to generate any additional SQL statements that you do not set.

The relationship between a UniDataAdapter and its corresponding UniCommandBuilder is always one-to-one. To create this correspondence, you set the DataAdapter property of the UniCommandBuilder object. This causes the UniCommandBuilder to register itself as a listener, which produces the output of UniDataAdapter.RowUpdating events that affect the System.Data.DataSet.

To generate INSERT, UPDATE, or DELETE statements, the UniCommandBuilder uses the UniDataAdapter.SelectCommand property to retrieve a required set of metadata. If you change the value of UniDataAdapter.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 UniCommandBuilder also uses the Connection, CommandTimeout, and Devart.Common.DbCommand.Transaction properties referenced by the UniDataAdapter.SelectCommand. The user should call Devart.Common.DbCommandBuilderBase.RefreshSchema if any of these properties are modified, or value of the UniDataAdapter.SelectCommand property itself is changed. Otherwise the UniDataAdapter.InsertCommand, UniDataAdapter.UpdateCommand, and UniDataAdapter.DeleteCommand properties retain their previous values.

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

Example
The following example uses UniCommand, along with UniDataAdapter and UniConnection, to select rows from a database. 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 table. The example then creates a UniCommandBuilder.
public DataSet SelectUniSrvRows(DataSet myDataSet,string myConnection,string mySelectQuery,string myTableName)
{
  UniConnection myConn = new UniConnection(myConnection);
  UniDataAdapter myDataAdapter = new UniDataAdapter();
  myDataAdapter.SelectCommand = new UniCommand(mySelectQuery, myConn);
  UniCommandBuilder myCommandBuilder = new UniCommandBuilder(myDataAdapter);

  myConn.Open();

  DataSet myDataSet = new DataSet();
  myDataAdapter.Fill(myDataSet, "Departments");

  //code to modify data in dataset here

  //Without the UniCommandBuilder this line would fail
  myDataAdapter.Update(myDataSet, "Departments");

  myConn.Close();

  return myDataSet;
}
Public Function SelectUniSrvRows(myDataSet As DataSet, myConnection As String, mySelectQuery As String, myTableName As String) As DataSet
  Dim myConn As New UniConnection(myConnection)
  Dim myDataAdapter As New UniDataAdapter()
  myDataAdapter.SelectCommand = New UniCommand(mySelectQuery, myConn)
  Dim myCommandBuilder As UniCommandBuilder = New UniCommandBuilder(myDataAdapter)

  myConn.Open()

  Dim myDataSet As DataSet = New DataSet
  myDataAdapter.Fill(myDataSet, "Departments")

  ' Code to modify data in DataSet here

  ' Without the UniCommandBuilder this line would fail.
  myDataAdapter.Update(myDataSet, "Departments")

  myConn.Close()

  SelectUniSrvRows = myDataSet
End Function
Inheritance Hierarchy

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Data.Common.DbCommandBuilder
            Devart.Common.DbCommandBuilderBase
               Devart.Common.DbCommandBuilder
                  Devart.Data.Universal.UniCommandBuilder

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