Take advantage of Azure Table storage in your .Net applications to store large amounts of key-value data in the Microsoft cloud Credit: Thinkstock Microsoft’s Azure cloud computing platform provides interoperable cloud computing services that are comprised of both open source and standards-based technologies. You can use an Azure storage account to work with all kinds of data including files, blobs, queues, and tables. Azure Table storage is a scalable, non-relational, key-value storage system that you can leverage to store large volumes of data in the cloud. This article presents a discussion of Azure Table storage and how we can work with it in .Net. Create a console application project in Visual Studio 2017 First off, let’s create a console application project in Visual Studio. If Visual Studio 2017 is up and running in your system, follow the steps outlined below to create the project. Launch the Visual Studio 2017 IDE. Click on File > New > Project. Select “Console App (.Net Framework)” from the list of templates displayed. Specify a name for the project. Specify the location for the project. Select the Framework version you would like to use. Click OK to save the project. Following these steps will result in a new console application project in Visual Studio. You can use this project to write and execute the programs given in the sections that follow. Create a new Azure storage account To work with blobs, files, tables, or queues in Azure, you will need an Azure storage account. An Azure storage account is a unique namespace in the Azure cloud that can be used for storage and retrieval of data. Naturally, you will first need to create an Azure account (if you don’t have one) and then use it to create an Azure storage account. If you don’t have an Azure account, you can create a free Azure account here. Once you have an Azure account, you can follow the steps given below to create a storage account in Azure: Open the Azure Portal. Click “Create a resource” and select “Storage account.” Specify the subscription and resource group. Specify the name and location for the storage account. Accept the default values for performance (“Standard”), account kind (“StorageV2”), replication (“RA-GRS”), and access tier (“Hot”). Click the “Next: Advanced >” button. Specify the details as necessary in the “Advanced” and “Tags” tabs (the defaults are OK for our purposes). Click “Next: Review + Create” to create the storage account. IDG The final step in creating your Azure storage account—review the settings and click Create. Now that your Azure storage account has been created, you can use it to work with Azure Tables. Create a new Azure Table using C# Select the console project you created earlier and add the following two NuGet packages from the NuGet package manager. WindowsAzure.Storage Microsoft.WindowsAzure.ConfigurationManager The following code snippet illustrates how you can connect to the Azure storage account we created above and then create a storage table if it doesn’t exist. static void Main(string[] args) { string connectionString = ConfigurationManager.AppSettings.Get("TableStorageConnectionString"); CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(connectionString); CloudTableClient cloudTableClient = cloudStorageAccount.CreateCloudTableClient(); CloudTable cloudTable = cloudTableClient.GetTableReference("DemoTable"); cloudTable.CreateIfNotExists(); Console.ReadKey(); } When you execute the above code, a new table named DemoTable will be created if none exists. You can log into your Azure account and view the table you’ve just created from the Azure Storage Explorer, which is available as a free download for Windows, MacOS, and Linux. Create an entity in an Azure Table using C# Inside an Azure Table, you store entities. An entity must have three mandatory properties, namely PartitionKey, RowKey, and Timestamp. To create an entity class, you should create a class that extends the TableEntity class pertaining to the Microsoft.WindowsAzure.Storage namespace. Here is an example of a typical entity. public class AuthorEntity : TableEntity { public AuthorEntity(int id, string firstName, string lastName) { Id = id; FirstName = firstName; LastName = lastName; PartitionKey = id.ToString(); RowKey = firstName + lastName; } public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } The following code snippet illustrates how you can store the entity in the Azure Table. static void Main(string[] args) { string connectionString = ConfigurationManager.AppSettings.Get("TableStorageConnectionString"); CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(connectionString); CloudTableClient cloudTableClient = cloudStorageAccount.CreateCloudTableClient(); CloudTable cloudTable = cloudTableClient.GetTableReference("DemoTable"); cloudTable.CreateIfNotExists(); var author = new AuthorEntity(1, "Joydip", "Kanjilal"); TableOperation tableOperation = TableOperation.Insert(author); cloudTable.Execute(tableOperation); Console.ReadKey(); } Read an entity from an Azure Table using C# To retrieve an entity, you will need to acquire a CloudTable reference and then use it to call the Execute method to run the TableOperation. The following code snippet illustrates how you can read data from Azure Table storage. Note the usage of the TableOperation.Retrieve method. string connectionString = ConfigurationManager.AppSettings.Get("TableStorageConnectionString"); CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(connectionString); CloudTableClient cloudTableClient = cloudStorageAccount.CreateCloudTableClient(); CloudTable cloudTable = cloudTableClient.GetTableReference("DemoTable"); TableOperation tableOp = TableOperation.Retrieve<AuthorEntity>("3", "JoydipKanjilal"); TableResult tr = cloudTable.Execute(tableOp); var data = tr.Result as AuthorEntity; Update an entity in an Azure Table using C# To update an entity in an Azure Table, you will first need to retrieve an instance of TableOperation as shown in the code snippet below. var partitionKey = "1"; var rowKey = "JoydipKanjilal"; TableOperation tableOp = TableOperation.Retrieve<AuthorEntity>(partitionKey, rowKey); Next, you should execute the table operation and retrieve the entity instance as shown in this code snippet. TableResult result = cloudTable.Execute(tableOp); var data = result.Result as AuthorEntity; Now that you have retrieved the entity instance, you write your code to update the entity and invoke the Replace method of TableOperation class to update the entity as shown below. TableResult result = cloudTable.Execute(tableOp); var data = result.Result as AuthorEntity; //Write code here to update the author entity. if (result != null) { TableOperation updateOp = TableOperation.Replace(data); cloudTable.Execute(updateOp); } Delete an entity from an Azure Table using C# To delete an entity, you follow the same approach as when updating an entity except that you will need to call the Delete method on the TableOperation class. var partitionKey = "1"; var rowKey = "JoydipKanjilal"; TableOperation tableOp = TableOperation.Retrieve<AuthorEntity>(partitionKey, rowKey); TableResult result = cloudTable.Execute(tableOp); var data = result.Result as AuthorEntity; if (result != null) { TableOperation deleteOp = TableOperation.Delete(data); cloudTable.Execute(deleteOp); } In Microsoft Azure jargon, a table implies a group of entities. Azure Table storage enables you to store and manage entities. In Azure Table storage, an entity is a row of data and a table is a group of entities. While there is no strict limit on the number of tables you may have or the amount of data that a table may contain, Microsoft provides Azure storage scale targets for optimal performance. The Azure Table storage scale targets list 500TB as the maximum size for a table and 1MB as the maximum size for an entity. Related content analysis Strategies to navigate the pitfalls of cloud costs Cloud providers waste a lot of their customers’ cloud dollars, but enterprises can take action. By David Linthicum Nov 15, 2024 6 mins Cloud Architecture Cloud Management Cloud Computing analysis Understanding Hyperlight, Microsoft’s minimal VM manager Microsoft is making its Rust-based, functions-focused VM tool available on Azure at last, ready to help event-driven applications at scale. By Simon Bisson Nov 14, 2024 8 mins Microsoft Azure Rust Serverless Computing how-to Docker tutorial: Get started with Docker volumes Learn the ins, outs, and limits of Docker's native technology for integrating containers with local file systems. By Serdar Yegulalp Nov 13, 2024 8 mins Devops Cloud Computing Software Development news Red Hat OpenShift AI unveils model registry, data drift detection Cloud-based AI and machine learning platform also adds support for Nvidia NIM, AMD GPUs, the vLLM runtime for KServe, KServe Modelcars, and LoRA fine-tuning. By Paul Krill Nov 12, 2024 3 mins Generative AI PaaS Artificial Intelligence Resources Videos