Insert Data Into Azure Table Storage Using ASP.NET Core Application

Today, in this article we will discuss how to develop a web application or console-based application to store data into the Azure Table Storage. Now, as we all know, Table Storage is a part of the Azure Storage. So, first, we will discuss some basic concepts about Azure table storage and then we will discuss how to store data into the Azure table storage.
 
Microsoft provides four different types of storage within the Azure Storage except for Azure SQL and Azure Cosmos DB. Azure Storage accounts provide four different types of data services through which we can save data into the Azure storage accounts. The below image shows all the elements of Azure Storage.
 
Insert Data Into Azure Table Storage Using ASP.NET Core Application

Concept of Azure Table Storage

 
Azure Table Storage is used to structure NoSql Data. With the help of Azure Table Storage, we can store a large amount of structured data. The Azure Table Storage is a NoSQL data service that always accepts authenticated calls either from Azure Cloud-based applications or outside applications. We can use Azure Tables for storing structured, non-relational data. Some of the common uses of the Azure Table Storage are,
  1. It can store TBs of structured NoSQL based data for service web-scale applications
  2. It can store datasets that do not require complex joins, foreign keys, or stored procedures and can be denormalized for fast access.
  3. It can quickly be querying data using a clustered index
  4. We can access the data of Azure Table Storage using the OData protocol and LINQ queries with the help of WCF Data Service and .Net Libraries.
  5. To Add Data, we just need to have the Storage Explorer or add data via a .Net program.
If we graphically represent the Azure Table Storage, then the Image will be as below,
 
Insert Data Into Azure Table Storage Using ASP.NET Core Application
Azure Table storage always contains the following components,
  1. URL Format
    Every Azure Table Storage Objects always has an URL type access point like http://<storage account>.table.core.windows.net/<table>. We can access the Azure table via these URLs with the help of the OData Protocol.

  2. Accounts
    Every Azure Table Storage object must be a part of the Azure Storage accounts.

  3. Table
    A table always represents either a collection of entities or a single entity. There is no requirement of defining a schema for defining the Table. So, it means that we can store data with a different set of properties within the Table.

  4. Entity
    Entity means a set of data with a single or multiple properties. It is just similar to the Database row. In Azure, normally each entity can be stored data up to 1 MB Size.

  5. Properties
    A property is a name-value pair. We can define up to 252 different properties within a single entity. Excluding the user-defined properties, each entity always contains three default system generated properties namely Partition Key, Row Key, and a Timestamp. Entities that contain the same partition key will be fetched faster when we run any query operation against the Azure Table Storage. In the entity, the row key is the unique identifier within the partition key values.
Insert Data Into Azure Table Storage Using ASP.NET Core Application
Perquisites Required
  • Microsoft Visual Studio 2017
  • The account in Azure Portal.
If you don’t have any existing Azure account, then you can create a free trial account in the Azure portal using your email id.
 

How to Create an Azure Storage Account?

 
To create the Azure Storage Account in Azure Portal, first, we need to login to the Azure Portal and then perform the below steps to create an Azure Storage Account.
 
Step 1
 
Click on the Azure Storage Accounts option from the Resource Dashboard
 
Insert Data Into Azure Table Storage Using ASP.NET Core Application
 
Step 2
 
On the Create Azure Storage Account, Click on the Add Button in the Storage Account Page.
 
Step 3
 
Now, provide the related required information to create an Azure Storage Account.
 
Insert Data Into Azure Table Storage Using ASP.NET Core Application
 
Step 4
 
In the above image, Storage Account Name means Account Name. Also, Select Storage V2 options to avail the service of all four data services of Azure Storage Accounts.
 
Step 5
 
Now click on review and create option.
 
Step 6
 
After the settings are validated, click Create to create the account
 
Step 7
 
Once the deployment is succeeded, click Go to resource options to open Storage Account.
 
Insert Data Into Azure Table Storage Using ASP.NET Core Application
 
Step 8
 
Once Storage Account is created, click on Access Keys options of the left panel and copy the ConnectionString value. This value needs to be provided in our web application so that we can upload files in Azure blob storage.
 
Insert Data Into Azure Table Storage Using ASP.NET Core Application
 

Create Console Application Using Asp.Net Core in VS 2019

 
Step 1
 
Now open Microsoft Visual Studio 2017 and click on File -> New -> Projects.
 
Step 2
 
Select Console App Project template and Click on Ok Button
 
Step 3
 
Now, we need to install the below Nuget Packages to access Azure storage account using Microsoft Azure Storage Cosmos Table client driver.
 
Insert Data Into Azure Table Storage Using ASP.NET Core Application
 
Step 4
 
Now, first, we need to establish communication between our application and the Azure Storage account. For that purpose, we will create a CloudStorageAccount instance and parse the connection string to the class instance. After it, we need to create the instance of CloudTableClient so that we can establish a connection with Azure Storage accounts. Now after this, we need to create an instance of CloudTable which will provide us a reference of Table Objects in the Azure Table storage.
  1. static string storageconn = "DefaultEndpointsProtocol=https;AccountName=demofilestoragesamples20;AccountKey=fw/Z6ou//OXhfIvwe8fZsHna3yJrW92iGOSJH/3hGkekhtxmg==;EndpointSuffix=core.windows.net";  
  2.   
  3.         static string table1 = "Employee";  
  4.         static string partitionkey = "Debasis Saha";  
  5.         static string rowKey = "userC";  
  6.          
  7.         static void Main(string[] args)  
  8.         {  
  9.             CloudStorageAccount storageAcc = CloudStorageAccount.Parse(storageconn);  
  10.             CloudTableClient tblclient = storageAcc.CreateCloudTableClient(new TableClientConfiguration());  
  11.             CloudTable table = tblclient.GetTableReference(table1);  
  12.   
  13.             Console.ReadKey();  
  14.         }  
Step 5
 
Now, first, we will try to insert a single record into the Azure Table Storage named Employee. For that purpose, we will create the below method:-
  1. public static async Task<string> InsertTableEntity(CloudTable p_tbl)  
  2. {  
  3.     Employee entity = new Employee("Debasis Saha""userC");  
  4.     entity.Salary = 50000;  
  5.     TableOperation insertOperation = TableOperation.InsertOrMerge(entity);  
  6.     TableResult result = await p_tbl.ExecuteAsync(insertOperation);  
  7.     Console.WriteLine("Employee Added");  
  8.     return "Employee added";  
  9. }  
Step 6
 
Now run the application and then check the Storage Explorer in the Azure Portal:-
 
Insert Data Into Azure Table Storage Using ASP.NET Core Application
 
Step 7
 
Now, if we want to insert bulk data then we can use the below method,
  1. public static async Task<string> InsertBatch(CloudTable p_tbl)  
  2. {  
  3.     TableBatchOperation l_batch = new TableBatchOperation();  
  4.     // All of the records should have the same partition key  
  5.     Employee entity1 = new Employee("Swapan Sharma""user1");  
  6.     entity1.Salary = 35000;  
  7.     Employee entity2 = new Employee("Swapan Sharma""userB");  
  8.     entity2.Salary = 20000;  
  9.     Employee entity3 = new Employee("Swapan Sharma""userC");  
  10.     entity3.Salary = 30000;  
  11.     l_batch.Insert(entity1);  
  12.     l_batch.Insert(entity2);  
  13.     l_batch.Insert(entity3);  
  14.     p_tbl.ExecuteBatch(l_batch);  
  15.     Console.WriteLine("Records Inserted");  
  16.     return "Completed";  
  17. }  
Step 8
 
Now after executing the program, check the Storage Explorer to view the data.
 
Insert Data Into Azure Table Storage Using ASP.NET Core Application
 
Step 9
 
Now, if we want to query the table against any particular partition key, then the below method needs to be executed-
  1. public static async Task<string> Query(CloudTable p_tbl)  
  2.         {  
  3.   
  4.             TableQuery<Employee> CustomerQuery = new TableQuery<Employee>().Where(  
  5.                 TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Swapan Sharma"  
  6.                 ));  
  7.             var itemlist = p_tbl.ExecuteQuery(CustomerQuery);  
  8.             foreach (Employee obj in itemlist)  
  9.             {  
  10.                 Console.WriteLine("The Employee Name is " + obj.PartitionKey);  
  11.                 Console.WriteLine("The User Name is " + obj.RowKey);  
  12.                 Console.WriteLine("The Salary is " + obj.Salary);  
  13.             }  
  14.             return "Operation complete";  
  15.         }  
Step 10
 
Now run the program to check the output,
 
Insert Data Into Azure Table Storage Using ASP.NET Core Application
 

Conclusion

 
In this article, we discussed how to store data using the Asp.Net Core application in Azure Table Storage. Any suggestions or feedback or query related to this article are most welcome.


Similar Articles