Q82. How to Update records in a table storage?
To update an entity, we use the Replace table operation. The first step is to retrieve the entity from the table and change the values to the required properties and perform the Replace operation.
We can also update an entity using Insert-or-Replace method.
For the Replace operatin to be successful, we must retrieve the entity from the server. The problem with Replace operation is that sometimes we do not know whether the entity exists in the server and also the entity has been changed since it was retrieved. In such a scenario, the Replace operation will return an error and the Insert-or-Replace is useful. This operation inserts an entity if there is no entity and the entity is replaced even though update was made since we retrieved it.
//Retrieve the storage account from the connection string. CloudStorageAccount storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("StorageConnectionString")); //Create the table client CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); //Create the CloudTable object that represents the "items" table. CloudTable table = tableClient.GetTableReference("items"); // Create a retrieve operation that takes an item entity. TableOperation retrieveOperation = TableOperation.Retrieve<Item>("RawMaterial","R001"); // Execute the operation. TableResult retrievedResult = table.Execute(retrieveOperation);
We can also update an entity using Insert-or-Replace method.
Insert-or-Replace method
For the Replace operatin to be successful, we must retrieve the entity from the server. The problem with Replace operation is that sometimes we do not know whether the entity exists in the server and also the entity has been changed since it was retrieved. In such a scenario, the Replace operation will return an error and the Insert-or-Replace is useful. This operation inserts an entity if there is no entity and the entity is replaced even though update was made since we retrieved it.
// Retrieve the storage account from the connection string CloudStorageAccount storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("StorageConnectionString")); // Create the table client CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); // Create the CloudTable object that represents the "items" table CloudTable table = tableClient.GetTableReference("items"); // Create a retrieve operation that takes a item entity TableOperation retrieveOperation = TableOperation.Retrieve<Item>("RawMaterial", "R001"); //Execute the operation TableResult retrievedResult = table.Execute(retrieveOperation); // Assign the result to a Item object. ItemEntity updateEntity = (ItemEntity)retrievedResult.Result; if (updateEntity != null) { //Change the description updateEntity.Description = "in nos"; // Create the InsertOrReplace TableOperation TableOperation insertOrReplaceOperation = TableOperation.InsertOrReplace(updateEntity); // Execute the operation. table.Execute(insertOrReplaceOperation); Console.WriteLine("Entity was updated."); }
See More Questions and Answers on - Azure Table Storage (cont..)
- How to access the Azure table storage from a .NET application?
- What is an Azure storage connection string?
- How to configure Azure connection strings?
- How to configure Azure connection strings for connecting to the storage emulator?
- How to create a table in Azure storage?
- How to create a table using a simple c# console application?
- How to insert entities into the Azure table?
- How do I retrieve a single entity from table storage using tableoperation?
- How do I query azure table storage using TableQuery class?