dotConnect for Dynamics 365 Documentation
Updating Data

You can update the Dynamics 365 data either by modifying data returned by the DynamicsDataAdapter class and then calling its Update method or by performing corresponding DML statements (INSERT, DELETE, UPDATE) via DynamicsCommand.

Here is an example showing how to update Dynamics 365 data using DynamicsDataAdapter.

using Devart.Data.Dynamics;
...
class Program
{

    static void Main(string[] args) {

        const string connectionString = "Server=https://your_company.crm4.dynamics.com;User Id=Test@Test.onmicrosoft.com;Password=A123456789;";
        const string sql = "SELECT ContactID, LastName FROM Contact";

        using (DynamicsConnection connection = new DynamicsConnection(connectionString)) {

            connection.Open();
            DataTable table = new DataTable("Contact");

            using (DynamicsCommand command = connection.CreateCommand()) {

                command.CommandText = sql;
                using (DynamicsDataAdapter adapter = new DynamicsDataAdapter(command)) {

                    adapter.Fill(table);

                    adapter.UpdateCommand = new DynamicsCommand("UPDATE Contact SET LastName = @name WHERE ContactID = @id", connection);

                    adapter.UpdateCommand.Parameters.Add("id", DbType.String).SourceColumn = "ContactID";
                    adapter.UpdateCommand.Parameters["id"].SourceVersion = DataRowVersion.Original;
                    adapter.UpdateCommand.Parameters.Add("name", DbType.String).SourceColumn = "LastName";

                    DataRow firstrow = table.Rows[0];
                    firstrow["LastName"] = "sample name 1";
                    Console.WriteLine(adapter.Update(table));
                }
            }
            Console.WriteLine("Rows after update.");

            foreach (DataRow row in table.Rows) {
                Console.WriteLine("{0}\t{1}", row[0], row[1]);
            }
        }
        Console.ReadKey();
    }

}
Imports Devart.Data.Dynamics
...
Module Module1

    Sub Main()

        Const connectionString As String = "Server=https://your_company.crm4.dynamics.com;User Id=Test@Test.onmicrosoft.com;Password=A123456789;"
        Const sql As String = "SELECT ContactID, LastName FROM Contact"

        Using connection As New DynamicsConnection(connectionString)

            connection.Open()
            Dim table As New DataTable("Accounts")

            Using command As DynamicsCommand = connection.CreateCommand()

                command.CommandText = sql
                Using adapter As New DynamicsDataAdapter(command)
                    adapter.Fill(table)

                    adapter.UpdateCommand = New DynamicsCommand("UPDATE Contact SET LastName = @name WHERE ContactID = @id", connection)

                    adapter.UpdateCommand.Parameters.Add("id", DbType.String).SourceColumn = "ContactID"
                    adapter.UpdateCommand.Parameters("id").SourceVersion = DataRowVersion.Original
                    adapter.UpdateCommand.Parameters.Add("name", DbType.String).SourceColumn = "LastName"

                    Dim firstrow As DataRow = table.Rows(0)
                    firstrow("LastName") = "sample name 1"
                    Console.WriteLine(adapter.Update(table))

                    Console.WriteLine("Rows after update.")
                    For Each row As DataRow In table.Rows
                        Console.WriteLine(row(0).ToString() & vbTab & row(1))
                    Next
                End Using
            End Using
        End Using
        Console.ReadKey()
    End Sub

End Module

The following example updates Dynamics 365 data using DynamicsCommand.

using Devart.Data.Dynamics;
...
class Program
{

    static void Main(string[] args) {

        const string connectionString = "Server=https://your_company.crm4.dynamics.com;User Id=Test@Test.onmicrosoft.com;Password=A123456789;";
        const string sql = "UPDATE Contact SET Department = 'Development' WHERE LastName = 'sample name 1'";

        using (DynamicsConnection connection = new DynamicsConnection(connectionString)) {

            connection.Open();
            using (DynamicsCommand command = connection.CreateCommand()) {

                command.CommandText = sql;
                Console.WriteLine(command.ExecuteNonQuery());
            }
        }
        Console.ReadKey();
    }
}
Imports Devart.Data.Dynamics
...
Module Module1

    Sub Main()

        Const connectionString As String = "Server=https://your_company.crm4.dynamics.com;User Id=Test@Test.onmicrosoft.com;Password=A123456789;"
        Const sql As String = "UPDATE Contact SET Department = 'Development' WHERE LastName = 'sample name 1'"

        Using connection As New DynamicsConnection(connectionString)

            connection.Open()
            Using command As DynamicsCommand = connection.CreateCommand()

                command.CommandText = sql
                Console.WriteLine(command.ExecuteNonQuery())
            End Using
        End Using
        Console.ReadKey()
    End Sub

End Module

See Also

Entity Framework  | Retrieving Data