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.
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.
- public class TableStorageController : Controller
- {
- public ActionResult MyTableCreation()
- {
- CloudStorageAccount userstorageAccount = CloudStorageAccount.Parse
- (CloudConfigurationManager.GetSetting("mystoragecontainer002_AzureStorageConnectionString"));
-
- CloudTableClient myTableClient = userstorageAccount.CreateCloudTableClient();
-
- CloudTable cricketTable = myTableClient.GetTableReference("cricketers");
-
- ViewBag.CreateSuccess= cricketTable.CreateIfNotExists();
-
-
- ViewBag.YourTableName = cricketTable.Name;
-
- return View();
- }
- }
Add following namespaces into your Controller
- using Microsoft.Azure;
- using Microsoft.WindowsAzure.Storage;
- using Microsoft.WindowsAzure.Storage.Table;
If you face any problem then you can refer to this screenshot.
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
- @{
- ViewBag.Title = "MyTableCreation";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
-
- <h2>Table Created</h2>
- <h3>
- Hey This Table <b style="color:red"> "@ViewBag.YourTableName" </b>
- @(ViewBag.CreateSuccess == true ? "Has Been Successfully Created." : "Already Exist Into Your Storage Account.☻")
- </h3><br />
Open _Layout.chtml and add another Action Link as shown below.
Run your application now. You’ll see there is a "Create Table" link to create a table. Click on it.
The result is on your screen. The table has been created.
You can check it in Azure Portal.
When you try to upload again with same name. It will produce the following result.
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.
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.
- public class CricketerEntity: TableEntity
- {
- public CricketerEntity(string lastName, string firstName)
- {
-
- this.PartitionKey = lastName;
- this.RowKey = firstName;
- }
-
- public CricketerEntity() { }
-
- public string BatingStyle { get; set; }
-
- public string Rank { get; set; }
-
- public string Runs { get; set; }
- }
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.
- public ActionResult CreateEntity()
- {
- CloudStorageAccount userstorageAccount = CloudStorageAccount.Parse
- (CloudConfigurationManager.GetSetting("mystoragecontainer002_AzureStorageConnectionString"));
-
- CloudTableClient myTableClient = userstorageAccount.CreateCloudTableClient();
-
- CloudTable cricketTable = myTableClient.GetTableReference("cricketers");
-
- CricketerEntity cricketer1 = new CricketerEntity("A.B", "Divillers");
- cricketer1.BatingStyle = "Right Hand Batsman";
- cricketer1.Rank = "1";
- cricketer1.Runs = "5000";
-
-
- TableOperation insertOperation = TableOperation.Insert(cricketer1);
-
- cricketTable.Execute(insertOperation);
-
- string getTable = Session["getTable"].ToString();
- ViewBag.YourTableName = getTable;
-
- return View();
- }
Take table name into session in the MyTableCreation method.
- Session["getTable"] = cricketTable.Name;
Right click on CreateEntity method to add a view and write following script on to it.
- @{
- ViewBag.Title = "CreateEntity";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
-
- <h2>Table Entity</h2>
-
- <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.
Now, run your application and check the output.
Click on the created Entity.
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.
Table has been created under selected container.
Click to view your table entity.
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.
- public ActionResult CreateBatchEntity()
- {
- CloudStorageAccount userstorageAccount = CloudStorageAccount.Parse
- (CloudConfigurationManager.GetSetting("mystoragecontainer002_AzureStorageConnectionString"));
-
- CloudTableClient myTableClient = userstorageAccount.CreateCloudTableClient();
-
- CloudTable cricketTable = myTableClient.GetTableReference("cricketers");
- TableBatchOperation batchOperation = new TableBatchOperation();
-
-
- CricketerEntity cricketer1 = new CricketerEntity("Mike", "Hussy");
- cricketer1.BatingStyle = "Left Hand Batsman";
- cricketer1.Rank = "2";
- cricketer1.Runs="4500";
-
- CricketerEntity cricketer2 = new CricketerEntity("Mike", "Henry");
- cricketer2.BatingStyle = "Right Hand batsman";
- cricketer2.Rank = "3";
- cricketer2.Runs = "6500";
-
-
- batchOperation.Insert(cricketer1);
- batchOperation.Insert(cricketer2);
-
-
- cricketTable.ExecuteBatch(batchOperation);
-
- string getTable = Session["getTable"].ToString();
- ViewBag.YourTableName = getTable;
-
- return View();
-
- }
Add a view and right following script.
- @{
- ViewBag.Title = "UploadYourBlob";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- <h3>Uploaded.....☺</h3><br />
- <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.
Run your application and select the second Action Link.
After clicking on the link, your entities have been created to the tables.
Refresh your table and you’ll get the expected result.
In my next article, I’ll discuss other storage methods in Azure.