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.             CloudTableClient myTableClient = userstorageAccount.CreateCloudTableClient();
  8.                         CloudTable cricketTable = myTableClient.GetTableReference(“cricketers”);
  9.             ViewBag.CreateSuccess= cricketTable.CreateIfNotExists();
  10.             ViewBag.YourTableName = cricketTable.Name;
  11.             return View();
  12.         }
  13.     }

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. <h2>Table Created</h2>
  6. <h3>
  7.     Hey This Table <b style=“color:red”“@ViewBag.YourTableName” </b>
  8.     @(ViewBag.CreateSuccess == true ? “Has Been Successfully Created.” : “Already Exist Into Your Storage Account.☻”)
  9. </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.             this.PartitionKey = lastName;
  6.             this.RowKey = firstName;
  7.         }
  8.         public CricketerEntity() { }
  9.         public string BatingStyle { get; set; }
  10.         public string Rank { get; set; }
  11.         public string Runs { get; set; }
  12.     }

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.             CloudTableClient myTableClient = userstorageAccount.CreateCloudTableClient();
  6.             CloudTable cricketTable = myTableClient.GetTableReference(“cricketers”);
  7.             CricketerEntity cricketer1 = new CricketerEntity(“A.B”“Divillers”);
  8.             cricketer1.BatingStyle = “Right Hand Batsman”;
  9.             cricketer1.Rank = “1”;
  10.             cricketer1.Runs = “5000”;
  11.             TableOperation insertOperation = TableOperation.Insert(cricketer1);
  12.             cricketTable.Execute(insertOperation);
  13.             string getTable = Session[“getTable”].ToString();
  14.             ViewBag.YourTableName = getTable;
  15.             return View();
  16.         }

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. <h2>Table Entity</h2>
  6. <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.             CloudTableClient myTableClient = userstorageAccount.CreateCloudTableClient();
  6.             CloudTable cricketTable = myTableClient.GetTableReference(“cricketers”);
  7.             TableBatchOperation batchOperation = new TableBatchOperation();
  8.             CricketerEntity cricketer1 = new CricketerEntity(“Mike”“Hussy”);
  9.             cricketer1.BatingStyle = “Left Hand Batsman”;
  10.             cricketer1.Rank = “2”;
  11.             cricketer1.Runs=“4500”;
  12.             CricketerEntity cricketer2 = new CricketerEntity(“Mike”“Henry”);
  13.             cricketer2.BatingStyle = “Right Hand batsman”;
  14.             cricketer2.Rank = “3”;
  15.             cricketer2.Runs = “6500”;
  16.             batchOperation.Insert(cricketer1);
  17.             batchOperation.Insert(cricketer2);
  18.             // Execute the batch operation for both entities.
  19.             cricketTable.ExecuteBatch(batchOperation);
  20.             string getTable = Session[“getTable”].ToString();
  21.             ViewBag.YourTableName = getTable;
  22.             return View();
  23.         }

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.

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

%d bloggers like this: