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.
To a single data item to the Data store:
- Connect to the data system.
- Create a new instance of the data type you need.
- Set its fields to values.
- 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
To add multiple data items to the Data store:
- Connect to the data system.
- Create a new instance of enumerable list of the data type items you need.
- Iterate the list and set its fields to values.
- 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