dotConnect for Dynamics 365 Documentation
Devart.Data.Dynamics Namespace / DynamicsCommandBuilder Class
Members Example

In This Topic
    DynamicsCommandBuilder Class
    In This Topic
    Automatically generates single-table commands used to reconcile changes made to a System.Data.DataSet with the Dynamics CRM.
    Syntax
    'Declaration
     
    Public Class DynamicsCommandBuilder 
       Inherits Devart.Data.SqlShimCommandBuilder
       Implements System.ComponentModel.IComponentSystem.IDisposable 
    Remarks

    The DynamicsDataAdapter does not automatically generate the SQL statements required to reconcile changes made to a System.Data.DataSet associated with Dynamics CRM. However, you can create a DynamicsCommandBuilder object that generates SQL statements for single-table updates. After assigning the DynamicsDataAdapter to the DynamicsCommandBuilder, it begins to generate any additional SQL statements that you do not set.

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

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

    If you assign null to corresponding property of DynamicsDataAdapter, the DynamicsCommandBuilder will be disassociated from the DynamicsDataAdapter, 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 DynamicsCommand, along with DynamicsDataAdapter and DynamicsConnection, to select rows from Dynamics CRM. 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 Dynamics CRM table. The example then creates a DynamicsCommandBuilder.
    public DataSet SelectDynamicsSrvRows(DataSet myDataSet,string dynamicsConnection,string mySelectQuery,string myTableName)
    {
      DynamicsConnection myConn = new DynamicsConnection(dynamicsConnection);
      DynamicsDataAdapter myDataAdapter = new DynamicsDataAdapter();
      myDataAdapter.SelectCommand = new DynamicsCommand(mySelectQuery, myConn);
      DynamicsCommandBuilder dynamicsCommandBuilder = new DynamicsCommandBuilder(myDataAdapter);
    
      myConn.Open();
    
      DataSet myDataSet = new DataSet();
      myDataAdapter.Fill(myDataSet, "Departments");
    
      //code to modify data in dataset here
    
      //Without the DynamicsCommandBuilder this line would fail
      myDataAdapter.Update(myDataSet, "Departments");
    
      myConn.Close();
    
      return myDataSet;
    }
    Public Function SelectDynamicsSrvRows(myDataSet As DataSet, dynamicsConnection As String, mySelectQuery As String, myTableName As String) As DataSet
      Dim myConn As New DynamicsConnection(dynamicsConnection)
      Dim myDataAdapter As New DynamicsDataAdapter()
      myDataAdapter.SelectCommand = New DynamicsCommand(mySelectQuery, myConn)
      Dim dynamicsCommandBuilder As DynamicsCommandBuilder = New DynamicsCommandBuilder(myDataAdapter)
    
      myConn.Open()
    
      Dim myDataSet As DataSet = New DataSet
      myDataAdapter.Fill(myDataSet, "Departments")
    
      ' Code to modify data in DataSet here
    
      ' Without the DynamicsCommandBuilder this line would fail.
      myDataAdapter.Update(myDataSet, "Departments")
    
      myConn.Close()
    
      SelectDynamicsSrvRows = myDataSet
    End Function
    Inheritance Hierarchy

    System.Object
       System.MarshalByRefObject
          System.ComponentModel.Component
             System.Data.Common.DbCommandBuilder
                Devart.Common.DbCommandBuilderBase
                   Devart.Common.DbCommandBuilder
                      Devart.Data.SqlShimCommandBuilder
                         Devart.Data.Dynamics.DynamicsCommandBuilder

    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