Modifying data
Glossary Item Box
Examples of using the Update class to build queries for updating database records
You can download the package with the configuration web service implementing the cases described below using the following link.
In most cases, a request for modification should contain the “Where” condition, which specifies which records exactly should be modified. Otherwise, all records will be modified.
Example 1
public string GetSqlTextExample(string oldName, string newName) { var result = ""; var update = new Update(UserConnection, "Contact") .Set("Name", Column.Parameter(newName)) .Where("Name").IsEqual(Column.Parameter(oldName)); result = update.GetSqlText(); return result; }
Example 2
public string ChangeContactName(string oldName, string newName) { var update = new Update(UserConnection, "Contact") .Set("Name", Column.Parameter(newName)) .Where ("Name").IsEqual(Column.Parameter(oldName)); var cnt = update.Execute(); return $"Contacts {oldName} changed to {newName}. {cnt} rows affected."; }
Example 3
public string ChangeAllContactModifiedBy(string Name) { var update = new Update(UserConnection, "Contact") .Set("ModifiedById", new Select(UserConnection).Top(1) .Column("Id") .From("Contact") .Where("Name").IsEqual(Column.Parameter(Name))); var cnt = update.Execute(); return $"All contacts are changed by {Name} now. {cnt} rows affected."; }
In this example, the Where condition refers to the Select request. The Update request does not contain the Where condition, since indiscriminate modification is the goal.
See also: