AzureTipsAndTricks/blog/tip85.md

69 строки
2.6 KiB
Markdown
Исходник Обычный вид История

2020-10-16 02:29:48 +03:00
---
type: post
title: "Tip 85 - Updating an item from a Azure Storage Table"
excerpt: "Learn how to update an item from an Azure Storage Table"
2020-12-03 11:19:03 +03:00
tags: [Storage]
2020-10-16 02:29:48 +03:00
date: 2018-01-24 17:00:00
---
::: tip
2020-12-03 11:54:08 +03:00
:bulb: Learn more : [Azure storage account overview](https://docs.microsoft.com/azure/storage/common/storage-account-overview?WT.mc_id=docs-azuredevtips-azureappsdev).
2020-10-16 02:29:48 +03:00
:::
2020-12-03 11:19:03 +03:00
### Updating an item from a Azure Storage Table
2020-10-16 02:29:48 +03:00
In case you are new to the Azure Storage Tables, we've reviewed the following items this week:
* [Creating your first Azure Storage Table](https://microsoft.github.io/AzureTipsAndTricks/blog/tip82.html)
* [Adding an item to a Azure Storage Table](https://microsoft.github.io/AzureTipsAndTricks/blog/tip83.html)
* [Reading an item from a Azure Storage Table](https://microsoft.github.io/AzureTipsAndTricks/blog/tip84.html)
* [Today - Updating an item from a Azure Storage Table](https://microsoft.github.io/AzureTipsAndTricks/blog/tip85.html)
Today, we'll be taking a look at updating an item through C# code into an Azure Storage Table.
2020-10-16 02:29:48 +03:00
#### Getting Started
Open the C# Console application that we were working with [yesterday](https://microsoft.github.io/AzureTipsAndTricks/blog/tip84.html) and let's add a method to:
* Update an item based off of the table, RowKey and PartitionKey that we pass in.
#### Update an item
In our `Program.cs` file, we'll now add in a helper method that passes in a table, RowKey and PartitionKey and the new message that we want to use.
***Special thanks to Niko12 for his comment below which caused me to rewrite this method.***
```csharp
static void UpdateMessage(CloudTable table, string partitionKey, string rowKey, string newMessage)
{
Thanks entity = table.GetEntity<Thanks>(partitionKey, rowKey);
entity.Name = newMessage;
2020-10-16 02:29:48 +03:00
table.UpdateEntity(entity, ETag.All, TableUpdateMode.Replace);
2020-10-16 02:29:48 +03:00
}
```
In this example, once it performs the lookup, if it is not null, then we want to update the message with the one that we specify.
2020-10-16 02:29:48 +03:00
#### Putting it all together.
The **Main** method inside of the `Program.cs` file, we'll call our helper method.
2020-10-16 02:29:48 +03:00
```csharp
static void Main(string[] args)
{
var serviceClient = new TableServiceClient(ConfigurationManager.AppSettings["StorageConnection"]);
2020-10-16 02:29:48 +03:00
TableClient table = serviceClient.GetTableClient("thankfulfor");
2020-10-16 02:29:48 +03:00
table.CreateIfNotExists();
//added these lines
UpdateMessage(table, "ThanksApp", "I am thankful for the time with my family", "I am thankful for the time with my family and friends");
2020-10-16 02:29:48 +03:00
Console.ReadKey();
}
```
<img :src="$withBase('/files/azupdatetable1.gif')">