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).

Updating a Single Data Item

To update a data item in the Data store:

  1. Connect to the data system.
  2. Get an existing item of the data type you need by querying the data store.
  3. Access its fields and change their values.
  4. 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

 

Updating Multiple Data Items

To add multiple data items to the Data store:

  1. Connect to the data system.
  2. Get an enumerable list of existing items of the data type you need by querying the data store.
  3. Iterate the list and change values in the fields of its items.
  4. 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