Azure Storage - Tables


Azure Table storage is a service that stores structured NoSQL data in the cloud, providing a key/attribute store with a schema less design.

Table Storage Concepts 

Table storage contains the following components,

Azure Storage - Tables
  • Accounts: All access to Azure Storage is done through a storage account. 
  • Table: A table is a collection of entities.
  • Entity: An entity is a set of properties, similar to a database row. 
  • Properties: A property is a name-value pair.

Create Azure Storage

From Azure Portal click on Create a Resource button
Azure Storage - Tables
From Azure marketplace select storage, then select storage account 
Azure Storage - Tables
Fill the storage account name and location, for other settings leave as default, then click review + create button. 

Connecting to Azure Storage 

Every request made against Microsoft Azure Storage must be authorized, unless the request is for a binary large object (blob) or container resource that has been made available for public or signed access.

One of the methods for authorizing access to a storage account is by using a connection string.

A connection string includes the authentication information required for your application to access data in an Azure storage account at run time.

To get the connection string open storage account, from settings click on Access Keys.
Azure Storage - Tables
Then copy connection string for Key1 or Key2
Azure Storage - Tables

Connecting to Storage by using .NET

To get started with managing storage, you will need the Microsoft.Azure.Storage.Common package
  1. Install-Package Microsoft.Azure.Storage.Common -Version -IncludePrerelease  

First, you will need to add the following using directives to the top of your class file:
  1. using Microsoft.Azure;   
  2. using Microsoft.Azure.Storage;   
 The most common way to connect to a storage account is to parse a connection string:
  1. var account = CloudStorageAccount.Parse("[connection string]");  

Accessing Storage Tables with the .NET SDK

You should use the Microsoft Azure CosmosDB Table library for .NET in common Azure Storage tables and Azure Cosmos DB Table API scenarios.
The package works with both the Azure Cosmos DB Table API and Azure Storage tables.

To get started, you will need the Microsoft.Azure.CosmosDB.Table package in your .NET project.
First, you will need to add the following using directive to the top of your class file:
  1. using Microsoft.Azure.CosmosDB.Table;  
The CloudTableClient class enables you to retrieve tables and entities stored in Storage tables. Here's one way to create the Storage tables client:
  1. var client = account.CreateCloudTableClient();   
Before you can manage a table, you need to get a reference to the table:
  1. var table = client.GetTableReference("Employee");   
For our first operation, we will create a table if it does not already exist:
  1. table.CreateIfNotExists();  
Entities map to C# objects by using a custom class derived from TableEntity.

To add an entity to a table, create a class that defines the properties of your entity. The following code defines an entity class that uses the employee's first name as the row key and last name as the partition key. Together, an entity's partition and row key uniquely identify it in the table.
  1. public class EmployeeEntity : TableEntity  
  2.  {  
  3.   public EmployeeEntity (string lastName, string firstName)   
  4.  {   
  5.   this.PartitionKey = lastName;   this.RowKey = firstName;  
  6.   }   
  7.   public EmployeeEntity () { }   
  8.   public string Email { getset; }   
  9.   public string PhoneNumber { getset; }   
  10. }  
Entities with the same partition key can be queried faster than entities with different partition keys, but using diverse partition keys allows for greater scalability of parallel operation.
Table operations that involve entities are performed via the CloudTable.

The following code example shows the creation of the CloudTable object and then of a EmployeeEntity object.
  1. EmployeeEntity employeeEntity = new EmployeeEntity("Sbeeh""Mohammad") {  
  2.     Email = "",  
  3.     PhoneNumber ="123456789"  
  5. };  
  7. TableOperation insertOperation = TableOperation.Insert(employeeEntity);  
  8. table.Execute(insertOperation);  

Querying Storage Tables by using the .NET SDK

To query a table for all entities in a partition, use a TableQuery object. The following code example specifies a filter for entities,
  1. var condition = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Sbeeh");   
  2. var query = new TableQuery<PersonEntity>().Where(condition);   
  3. var lst= table.ExecuteQuery(query);  
For full source code here is the full code for the MVC project.
Home Controller
  1. public ActionResult Index()  
  2.       {  
  3.           var account = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["azureStorageAccount"]);  
  4.           var client = account.CreateCloudTableClient();  
  6.           var table = client.GetTableReference("Employee");  
  8.           table.CreateIfNotExists();  
  10.           EmployeeEntity employeeEntity = new EmployeeEntity("Sbeeh""Mohammad") {  
  11.               Email = "",  
  12.               PhoneNumber ="123456789"  
  14.           };  
  16.           TableOperation insertOperation = TableOperation.Insert(employeeEntity);  
  17.           table.Execute(insertOperation);  
  18.           return View();  
  19.       }  
  21.       public ActionResult Employees()  
  22.       {  
  24.           var condition = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Sbeeh");  
  25.           var query = new TableQuery<EmployeeEntity>().Where(condition);  
  27.           var account = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["azureStorageAccount"]);  
  28.           var client = account.CreateCloudTableClient();  
  29.           var table = client.GetTableReference("Employee");  
  31.          var lst= table.ExecuteQuery(query);  
  34.           return View(lst);  
  35.       }  
Employees View
  1. @model IEnumerable<AzureTable.EmployeeEntity>  
  3. @{  
  4.     ViewBag.Title = "Employees";  
  5. }  
  8. @foreach (var item in Model)  
  9. {  
  10. <p>@item.RowKey @item.PartitionKey</p>  
  11. }  
Employee Entity Class
  1. public class EmployeeEntity : TableEntity  
  2. {  
  3.     public EmployeeEntity(string lastName, string firstName)  
  4.     {  
  5.         this.PartitionKey = lastName; this.RowKey = firstName;  
  6.     }  
  7.     public EmployeeEntity() { }  
  8.     public string Email { getset; }  
  9.     public string PhoneNumber { getset; }  
  10. }  
  1. <appSettings>  
  2.     <add key="azureStorageAccount" value="YourAzureStorageConnectionString" />  
  3.   </appSettings>  
 Thank you for reading. 

Recommended Ebook

Azure DevOps - Complete CI-CD Pipeline

Download Now!
Similar Articles