Overview Of Microsoft Azure Storage - Part Four

In my previous articles about Microsoft Azure Storage, I’ve discussed about Blob Storage in detail. As I explained, in first article of the series that Azure Storage has 4 types of Storage Blob, Table, Queue, and File Storage.

In this article, I’m going to explain about Table Storage in Azure. Table is simply structured data which can be accessed by ADO.NET Data Services. Azure Table Storage is non-relational, key value pair, storage system suitable for storing massive amounts of structured data. So Table can be useful for applications that must store large amounts of relational data and need additional structure for that data.

So the Table Storage stores structured data without schemas, it does not provide any way to represent relationship between the data.

So for the table, you must have a Storage Account. In Storage Account we have got the two separate tables and in those table we have got Entity and in Entities we have columns property. I’m going to develop same table storage structure through the Visual Studio 2015.

Azure

Let’s see how we can create Table Storage programmatically and how to save the data into that Table storage. I’m using Visual Studio 2015 and Azure Storage Account like previous article. Follow the same steps of previous article unless ready of Controller.

Step 1

Create Asp.net Web Project and a give a name to your project.

Step 2

Select MVC Template

Step 3

Right click on to Project and add on “Connected Services” to connect with your Azure Storage Account. (Follow Part-Three of the series article)

I hope your project is ready now. Right click on to controller’s folder to add a controller. Create a Method under your controller and parse the connection string to retrieve your account detail from key is in Web.Config file as it is previous article where we created a client of the table, here we returned client for table. Next create a CloudTable object that represents your table name. Create the table if it doesn't exist and take it into ViewBag for showing it in View result and table name as well as. 

  1. public class TableStorageController : Controller  
  2.     {        
  3.         public ActionResult MyTableCreation()  
  4.         {  
  5.             CloudStorageAccount userstorageAccount = CloudStorageAccount.Parse  
  6.                 (CloudConfigurationManager.GetSetting("mystoragecontainer002_AzureStorageConnectionString"));  
  7.   
  8.             CloudTableClient myTableClient = userstorageAccount.CreateCloudTableClient();  
  9.   
  10.                         CloudTable cricketTable = myTableClient.GetTableReference("cricketers");  
  11.   
  12.             ViewBag.CreateSuccess= cricketTable.CreateIfNotExists();  
  13.   
  14.               
  15.             ViewBag.YourTableName = cricketTable.Name;  
  16.   
  17.             return View();  
  18.         }  
  19.     }   

Add following namespaces into your Controller 

  1. using Microsoft.Azure;  
  2. using Microsoft.WindowsAzure.Storage;  
  3. using Microsoft.WindowsAzure.Storage.Table;   

If you face any problem then you can refer to this screenshot.

Azure

Table should definitely be created but I want to show the result of adding a View. Right click on your ActionResult method and add a View. Add the following snippet into view 

  1. @{  
  2.     ViewBag.Title = "MyTableCreation";  
  3.     Layout = "~/Views/Shared/_Layout.cshtml";  
  4. }  
  5.   
  6. <h2>Table Created</h2>  
  7. <h3>  
  8.     Hey This Table <b style="color:red""@ViewBag.YourTableName" </b>  
  9.     @(ViewBag.CreateSuccess == true ? "Has Been Successfully Created." : "Already Exist Into Your Storage Account.☻")  
  10. </h3><br />   

Azure

Open _Layout.chtml and add another Action Link as shown below.

Azure

Run your application now. You’ll see there is a "Create Table" link to create a table. Click on it.

Azure

The result is on your screen. The table has been created.

Azure

You can check it in Azure Portal.

Azure

When you try to upload again with same name. It will produce the following result.

Azure

Add an Entity to a Table

In this article, we will see how we can add an Entity and Batch of Entities in a Table. Create a Model class and define properties of entity.

Right click on Models folder and add a class CricketerEntity.cs.

Azure

I’ve a Cricketer Entity class that uses TableEntity which represents the base object type for a table entity in the Table Service. The class uses the cricketer’s first name and last name with row key and partition key respectively, with some other property. 

  1. public class CricketerEntity: TableEntity  
  2.     {  
  3.         public CricketerEntity(string lastName, string firstName)  
  4.         {  
  5.              
  6.             this.PartitionKey = lastName;  
  7.             this.RowKey = firstName;  
  8.         }  
  9.   
  10.         public CricketerEntity() { }  
  11.   
  12.         public string BatingStyle { get; set; }  
  13.   
  14.         public string Rank { get; set; }  
  15.   
  16.         public string Runs { get; set; }  
  17.     }   

Create a new ActionResult method for creating New Entity. Here, I need to retrieve my storage account for accessing cloud table. Create a new Cricketer entity with its respective properties.

After creating entity, you need to create the TableOperation object that would be inserted to your entity. Next, execute the insertOperation to insert an entity. At the end of the code, I’m getting the table name from MyTableCreation Method string of session, into ViewBag. 

  1. public ActionResult CreateEntity()  
  2.         {  
  3.             CloudStorageAccount userstorageAccount = CloudStorageAccount.Parse  
  4.                 (CloudConfigurationManager.GetSetting("mystoragecontainer002_AzureStorageConnectionString"));  
  5.   
  6.             CloudTableClient myTableClient = userstorageAccount.CreateCloudTableClient();  
  7.   
  8.             CloudTable cricketTable = myTableClient.GetTableReference("cricketers");  
  9.   
  10.             CricketerEntity cricketer1 = new CricketerEntity("A.B""Divillers");  
  11.             cricketer1.BatingStyle = "Right Hand Batsman";  
  12.             cricketer1.Rank = "1";  
  13.             cricketer1.Runs = "5000";  
  14.               
  15.              
  16.             TableOperation insertOperation = TableOperation.Insert(cricketer1);  
  17.   
  18.             cricketTable.Execute(insertOperation);  
  19.   
  20.             string getTable = Session["getTable"].ToString();  
  21.             ViewBag.YourTableName = getTable;  
  22.   
  23.             return View();  
  24.         }   

Take table name into session in the MyTableCreation method.

  1. Session["getTable"] = cricketTable.Name;  

Right click on CreateEntity method to add a view and write following script on to it. 

  1. @{  
  2.     ViewBag.Title = "CreateEntity";  
  3.     Layout = "~/Views/Shared/_Layout.cshtml";  
  4. }  
  5.   
  6. <h2>Table Entity</h2>  
  7.   
  8. <h3>The Entity is Successfully creating into <b style="color:red">"@ViewBag.YourTableName"</b> Table </h3>   

Now, go to MyTableCreation.chtml view to make a ActionLink for creating Entity.

Azure

Now, run your application and check the output.

Azure

Click on the created Entity.

Azure

Check your created Entity in Azure Table Storage. Click on the Server Explorer in your Visual Studio Project, click on Storage, and refresh your table.

Azure

Table has been created under selected container.

Azure

Click to view your table entity.

Azure

How can you Create Batch Entity into Table?

By performing Batch Operation, you can insert up to 100 entities in table. Create a new method for Batch Entity and create two cricketer entities. Then, add both in batch insert operation. After that, execute the batch operation method. 

  1. public ActionResult CreateBatchEntity()  
  2.         {  
  3.                         CloudStorageAccount userstorageAccount = CloudStorageAccount.Parse  
  4.                 (CloudConfigurationManager.GetSetting("mystoragecontainer002_AzureStorageConnectionString"));  
  5.   
  6.             CloudTableClient myTableClient = userstorageAccount.CreateCloudTableClient();  
  7.   
  8.             CloudTable cricketTable = myTableClient.GetTableReference("cricketers");  
  9.             TableBatchOperation batchOperation = new TableBatchOperation();  
  10.   
  11.               
  12.             CricketerEntity cricketer1 = new CricketerEntity("Mike""Hussy");  
  13.             cricketer1.BatingStyle = "Left Hand Batsman";  
  14.             cricketer1.Rank = "2";  
  15.             cricketer1.Runs="4500";  
  16.               
  17.             CricketerEntity cricketer2 = new CricketerEntity("Mike""Henry");  
  18.             cricketer2.BatingStyle = "Right Hand batsman";  
  19.             cricketer2.Rank = "3";  
  20.             cricketer2.Runs = "6500";  
  21.   
  22.               
  23.             batchOperation.Insert(cricketer1);  
  24.             batchOperation.Insert(cricketer2);  
  25.   
  26.             // Execute the batch operation for both entities.  
  27.             cricketTable.ExecuteBatch(batchOperation);  
  28.   
  29.             string getTable = Session["getTable"].ToString();  
  30.             ViewBag.YourTableName = getTable;  
  31.   
  32.             return View();  
  33.   
  34.         }   

Add a view and right following script. 

  1. @{  
  2.     ViewBag.Title = "UploadYourBlob";  
  3.     Layout = "~/Views/Shared/_Layout.cshtml";  
  4. }  
  5.     <h3>Uploaded.....☺</h3><br />  
  6.     <h2>This Blob <b style="color:purple""@ViewBag.BlobName" </b>@ViewBag.Message in <b style="color:red""@ViewBag.YourBlobContainerName" </b> </h2>   

To show an Action Link on MyTableCreation.cshtml page, update this by the following code.

Azure

Run your application and select the second Action Link.

Azure
After clicking on the link, your entities have been created to the tables.

Azure
Refresh your table and you’ll get the expected result.

Azure

In my next article, I’ll discuss other storage methods in Azure.


IotCoast2Coast
Improving the World of Tomorrow :- We offer application development and support services.