Last time in our discussion on creating Azure storage table, using our developer’s account, we have successfully deployed our Azure storage using the M.S. Azure portal. We also used a tool called Azure Storage Explorer in creating our sample Table which also contains sample Entities. For today’s article I will teach you on basic operations, e.g. (Insert, Select, delete) you can perform on your Azure Storage using a C# Console application.
Installing Azure Storage NuGet
First things first, we need to install the required NuGet in order to use Azure technology in our Console Application. Just open the Tools tab then NuGet package manager then click the Package Manager Console.
We need WindowsAzure.Storage because it contains the repositories needed to access data resources in our Storage Account. Just type in the Package Manager Console, the command based on the screenshot below:
We need Microsoft.WindowsAzure.ConfigurationManager because it contains a unified functionality for parsing the connection string from the configuration file regardless where it was deployed. Just type in the Package Manager Console, the command based on the screenshot below:
For our connection string we need to add application settings to our app.config file using our credentials from our previously created Azure Storage Table. Just replace the Key for your desired name for your b, AccountName and AccountKey based on your created Azure Table settings. Kindly see below for our sample App.config file:
Once we have set up our application configuration file, let’s proceed to the basics of Azure Table storage, e.g. (Create table, Insert, delete). You can later check the actual results for the codes below in your Azure Portal under Azure Storage Table or using the Azure Storage Explorer linked to your Azure account.
Creating Azure Table
These are the steps for creating a table in the Azure Table Storage:
1.) We need to parse from our application configuration file our Azure connection string. We need to declare an object of type CloudStorageAccount and will use it’s Parse method which will contain our parsed connection string using the CloundConfigurationManager.GetSetting() method.
2.) Next is to create CloudTableClient which will retrieve all tables and entities which are stored in our TableStorage.
3.) Next is to get the specific table that you want to reference using the CloudTable.
4.) Lastly, will just execute the CreateIfNotExists() method of CloudTable to create the table.
Take note that for the rest of the tutorial the above code is standard prior to the execution of CreateIfNotExists() method. So one way you can do is to reuse the creation of CloudStorageAccount right up to getting the table reference to be used.
Adding Entity to our table
Now that we have created our Table called “Employees” we will create our Entity using the steps below:
1.) First, we need to create a class which will contain our entity for our Table “Employees”. We need it to inherit from the TableEntity then also need to create an empty constructor.
2.) Let’s then create the Entity by populating the properties like the firstname, lastname, partitionkey and rowkey.
3.) Then let’s create a TableOperation object and use it’s Insert Method then pass it to the Execute method of our Cloud Table.
Adding Multiple Entities
Now that we have created our very first entity in our Azure Table, let me discuss with you on how you can insert multiple entities in one execution.
1.) Lets create two new employee entities.
2.) Then let’s use the TableBatchOperation instead of the TableOperation for multiple adding of entities. For TableBatchOperation we need to add the created entities using the Insert method.
3.) Then to insert our multiple entities to our table will just use the ExecuteBatch method of the table instead of the Execute method.
Retrieving Entities From Azure Storage
Now that we have learned how to insert either single or multiple entities to our azure storage, let’s now discuss on how to retrieve those records.
In our example, let’s just get all partition key which contains the IT data. We will just use the TableQuery object to construct the entity we want to retrieve and also the conditions we want to include.
Here is our sample output for the queried records:
Retrieving a Single Record
We can also retrieve a single record from our azure storage. Will just use the Retrieve method of the TableOperation using the parameter of the PartitionKey and RowKey. We should always check the retrieved result if it is null because after that we need to cast the returned result to our EmployeesEntity.
As shown below, we have cast our result to our EmployeesEntity and used the lastname to be displayed in our console.
Updating an Entity
For updating an Entity we just need first to retrieve the record, then create a new EmployeesEntity object set to the retrieved results from the Azure storage and set the desired property you want to update. For this sample we will try to update the Last Name of LUNA to DE LUNA.
Take note that in updating the record, you should always check if it is existing prior to executing the entity update.
Deleting an Entity
Finally, for deleting a record, it’s just like updating a record minus the setting of the property you want to update. We will just get the record you want to update then pass it to the Delete method of the TableOperation.
Like for update you also need to check prior to deleting a record if it is existing.
Now that we have discussed the basics of Azure Table storage, I think we need to discuss about the basics of LINQ so that we can manipulate our retrieved records from Azure table in our application side.
Happy Coding 🙂