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.

 Azure Storage

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 query 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.

 Azure Table

Azure Table storage always contains the following components.

  • URL Format: Every Azure Table Storage Objects always has a 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.
  • Accounts: Every Azure Table Storage object must be a part of the Azure Storage accounts.
  • Table: A table always represents either a collection of entities or a single entity. There is no requirement to define a schema for defining the Table. So, it means that we can store data with a different set of properties within the Table.
  • 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.
  • 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 a Partition Key, a 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.
    Property

Perquisites Required

  • Microsoft Visual Studio 2017
  • The account is in Azure Portal.

If you don’t have an 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.

Resource Dashboard

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.

Storage Account

Step 4. In the above image, the 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 the 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 the Storage Account.

Tables

Step 8. Once the Storage Account is created, click on the 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.

Access Keys

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 the Console App Project template and Click on the OK Button.

Step 3. Now, we need to install the below Nuget Packages to access Azure storage account using the Microsoft Azure Storage Cosmos Table client driver.

 Microsoft Azure

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.

static string storageconn = "DefaultEndpointsProtocol=https;AccountName=demofilestoragesamples20;AccountKey=fw/Z6ou//OXhfIvwe8fZsHna3yJrW92iGOSJH/3hGkekhtxmg==;EndpointSuffix=core.windows.net";  
static string table1 = "Employee";  
static string partitionkey = "Debasis Saha";  
static string rowKey = "userC";  
static void Main(string[] args)  
{  
    CloudStorageAccount storageAcc = CloudStorageAccount.Parse(storageconn);  
    CloudTableClient tblclient = storageAcc.CreateCloudTableClient(new TableClientConfiguration());  
    CloudTable table = tblclient.GetTableReference(table1);  
    Console.ReadKey();  
}  

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.

public static async Task<string> InsertTableEntity(CloudTable p_tbl)
{
    Employee entity = new Employee("Debasis Saha", "userC");
    entity.Salary = 50000;
    TableOperation insertOperation = TableOperation.InsertOrMerge(entity);
    TableResult result = await p_tbl.ExecuteAsync(insertOperation);
    Console.WriteLine("Employee Added");
    return "Employee added";
}

Step 6. Now run the application and then check the Storage Explorer in the Azure Portal.

Azure Portal

Step 7. Now, if we want to insert bulk data then we can use the below method.

public static async Task<string> InsertBatch(CloudTable p_tbl)
{
    TableBatchOperation l_batch = new TableBatchOperation();
    // All of the records should have the same partition key
    Employee entity1 = new Employee("Swapan Sharma", "user1");
    entity1.Salary = 35000;
    Employee entity2 = new Employee("Swapan Sharma", "userB");
    entity2.Salary = 20000;
    Employee entity3 = new Employee("Swapan Sharma", "userC");
    entity3.Salary = 30000;
    l_batch.Insert(entity1);
    l_batch.Insert(entity2);
    l_batch.Insert(entity3);
    p_tbl.ExecuteBatch(l_batch);
    Console.WriteLine("Records Inserted");
    return "Completed";
}

Step 8. Now after executing the program, check the Storage Explorer to view the data.

Step 9. Now, if we want to query the table against any particular partition key, then the below method needs to be executed.

public static async Task<string> Query(CloudTable p_tbl)  
{  
    TableQuery<Employee> CustomerQuery = new TableQuery<Employee>().Where(  
        TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Swapan Sharma"  
        ));  
    var itemlist = p_tbl.ExecuteQuery(CustomerQuery);  
    foreach (Employee obj in itemlist)  
    {  
        Console.WriteLine("The Employee Name is " + obj.PartitionKey);  
        Console.WriteLine("The User Name is " + obj.RowKey);  
        Console.WriteLine("The Salary is " + obj.Salary);  
    }  
    return "Operation complete";  
}  

Step 10. Now run the program to check the output.

Output

Conclusion

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


Similar Articles