dotConnect for SugarCRM Documentation
In This Topic
    Updating Data
    In This Topic

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

    Here is an example showing how to update SugarCRM data using SugarDataAdapter.

    using Devart.Data.Sugar;
    ...
    class Program
    {
    
        static void Main(string[] args) {
    
            var monitor = new Devart.Data.Sugar.SugarMonitor() { IsActive = true };
    
            const string connectionString = "server=https://your_company.sugaropencloud.eu;user id=admin;password=AmFFA4ZryM;";
            const string sql = "SELECT id, name FROM Campaigns";
    
            using (SugarConnection connection = new SugarConnection(connectionString)) {
    
                connection.Open();
                DataTable table = new DataTable("Campaigns");
    
                using (SugarCommand command = connection.CreateCommand()) {
    
                    command.CommandText = sql;
                    using (SugarDataAdapter adapter = new SugarDataAdapter(command)) {
    
                        adapter.Fill(table);
    
                        adapter.UpdateCommand = new SugarCommand("UPDATE Campaigns SET name = @name WHERE id = @id", connection);
     
                        adapter.UpdateCommand.Parameters.Add("id", DbType.String).SourceColumn = "id";
                        adapter.UpdateCommand.Parameters["id"].SourceVersion = DataRowVersion.Original;
                        adapter.UpdateCommand.Parameters.Add("name", DbType.String).SourceColumn = "name";
    
                        DataRow firstrow = table.Rows[0];
                        firstrow["name"] = "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]);
                }
            }
        }
    
    }
    
    
    Imports Devart.Data.Sugar
    ...
    Module Module1
    
        Sub Main()
    
            Const connectionString As String = "server=https://your_company.sugaropencloud.eu;user id=admin;password=AmFFA4ZryM;"
            Const sql As String = "SELECT id, name FROM Campaigns"
    
            Using connection As New SugarConnection(connectionString)
    
                connection.Open()
                Dim table As New DataTable("Campaigns")
    
                Using command As SugarCommand = connection.CreateCommand()
    
                    command.CommandText = sql
                    Using adapter As New SugarDataAdapter(command)
                        adapter.Fill(table)
    
                        adapter.UpdateCommand = New SugarCommand("UPDATE Campaigns SET name = @name WHERE id = @id", connection)
    
                        adapter.UpdateCommand.Parameters.Add("id", DbType.String).SourceColumn = "id"
                        adapter.UpdateCommand.Parameters("id").SourceVersion = DataRowVersion.Original
                        adapter.UpdateCommand.Parameters.Add("name", DbType.String).SourceColumn = "name"
    
                        Dim firstrow As DataRow = table.Rows(0)
                        firstrow("name") = "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
        End Sub
    
    End Module
    
    

    The following example updates SugarCRM data using SugarCommand.

    using Devart.Data.Sugar;
    ...
    class Program
    {
    
        static void Main(string[] args) {
    
            const string connectionString = "server=https://your_company.sugaropencloud.eu;user id=admin;password=AmFFA4ZryM;";
            const string sql = "UPDATE Campaigns SET end_date = '12/31/2015' WHERE name = 'sample name 1'";
    
            using (SugarConnection connection = new SugarConnection(connectionString)) {
    
                connection.Open();
                using (SugarCommand command = connection.CreateCommand()) {
    
                    command.CommandText = sql;
                    Console.WriteLine(command.ExecuteNonQuery());
                }
            }
        }
    }
    
    
    Imports Devart.Data.Sugar
    ...
    Module Module1
    
        Sub Main()
    
            Const connectionString As String = "server=https://your_company.sugaropencloud.eu;user id=admin;password=AmFFA4ZryM;"
            Const sql As String = "UPDATE Campaigns SET end_date = '12/31/2015' WHERE name = 'sample name 1'"
    
            Using connection As New SugarConnection(connectionString)
    
                connection.Open()
                Using command As SugarCommand = connection.CreateCommand()
    
                    command.CommandText = sql
                    Console.WriteLine(command.ExecuteNonQuery())
                End Using
            End Using
        End Sub
    
    End Module
    
    

    See Also

    Entity Framework  | Retrieving Data