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 Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools analysis Succeeding with observability in the cloud Cloud observability practices are complex—just like the cloud deployments they seek to understand. The insights observability offers make it a challenge worth tackling. By David Linthicum Nov 19, 2024 5 mins Cloud Management Cloud Computing news Akka distributed computing platform adds Java SDK Akka enables development of applications that are primarily event-driven, deployable on Akka’s serverless platform or on AWS, Azure, or GCP cloud instances. By Paul Krill Nov 18, 2024 2 mins Java Scala Serverless Computing 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 Resources Videos