Accessing Data with C#

How to Add Data

There are two overloaded Add methods to add data to the default Data storage. Before you add new data items, you should create its instance by using the static New method of DataConnection.

Adding a Single Data Item

To a single data item to the Data store:

  1. Connect to the data system.
  2. Create a new instance of the data type you need.
  3. Set its fields to values.
  4. Add the item to the data system.
using (DataConnection connection = new DataConnection())
{
   Demo.Users myUser = DataConnection.New<Demo.Users>();
   myUser.Id = Guid.NewGuid();
   myUser.Name = "John Doe";
   myUser = connection.Add<Demo.Users>(myUser); 
}

Listing 12: Adding a single data item

 

Add Multiple Data Items

To add multiple data items to the Data store:

  1. Connect to the data system.
  2. Create a new instance of enumerable list of the data type items you need.
  3. Iterate the list and set its fields to values.
  4. Add the list to the data system.
using (DataConnection connection = new DataConnection())
{
   List<Demo.Users> myUsers = new List<Demo.Users>();
   for (int i = 0; i < 10; i++)
   {
      Demo.Users myUser = DataConnection.New<Demo.Users>();
      myUser.Id = Guid.NewGuid();
      myUser.Name = "John Doe";
      myUser.Number = i;
      myUsers.Add(myUser);
   }   
   connection.Add<Demo.Users>(myUsers);
}

Listing 13: Adding multiple data items