C#:Basic Azure Storage Table Operations

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.

WindowsAzure.Storage

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:

WindowsAzureStorage

Microsoft.WindowsAzure.ConfigurationManager

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:

MicrosoftWindowAzureConfigurationManager.PNG

Connection String

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:

AppConfigAzureStorage.PNG

AzureKeys.PNG

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.

AzureCloudStorageAccountParse.PNG

2.) Next is to create CloudTableClient which will retrieve all tables and entities which are stored in our TableStorage.

AzureCloudTable.PNG

3.) Next is to get the specific table that you want to reference using the CloudTable.

AzureTableReference.PNG

4.) Lastly, will just execute the CreateIfNotExists() method of CloudTable to create the table.

AzureCreateTableIFNotExists.PNG

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.

AzureEmployeeEntity.PNG

2.) Let’s then create the Entity by populating the properties like the firstname, lastname, partitionkey and rowkey.

AzureCreateObject

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.

AzureInsertOperation.PNG

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.

AzureCreateMultipleEntity.PNG

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.

AzureTableBatchOperation.PNG

3.) Then to insert our multiple entities to our table will just use the ExecuteBatch method of the table instead of the Execute method.

AzureExecuteBatch.PNG

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.

AzureTableQuery.PNG

Here is our sample output for the queried records:

AzureConsoleOutput.PNG

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.

AzureRetrieveSingleRecord.PNG

As shown below, we have cast our result to our EmployeesEntity and used the lastname to be displayed in our console.AzureLunaOutput.PNG

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.

AzureUpdateEntity

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.

AzureDeleteEntity.PNG

Like for update you also need to check prior to deleting a record if it is existing.

Conclusion

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 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s