Accessing Data with C#
How to Update Data
There are two overloaded Update methods to update data in the Storage. Before updating existing data items, you need to get their instances (for example, by querying the data store).
To update a data item in the Data store:
- Connect to the data system.
- Get an existing item of the data type you need by querying the data store.
- Access its fields and change their values.
- Commit the changes in the updated item to the data system.
using (DataConnection connection = new DataConnection()) { Demo.Users myUser = (from d in connection.Get<Demo.Users>() where d.Name == "John Smith" select d).First(); myUser.Name = "John Doe"; connection.Update<Demo.Users>(myUser); }
Listing 14: Updating a single data item
To add multiple data items to the Data store:
- Connect to the data system.
- Get an enumerable list of existing items of the data type you need by querying the data store.
- Iterate the list and change values in the fields of its items.
- Commit the changes in the updated items to the data system
using (DataConnection connection = new DataConnection()) { var myUsers = connection.Get<Demo.Users>().Where (d => d.Number < 10).ToList(); foreach (Demo.Users myUser in myUsers) { myUser.Number += 10; } connection.Update<Demo.Users>(myUsers); }
Listing 15: Updating multiple data items