AzureTipsAndTricks/blog/tip83.md

96 строки
3.6 KiB
Markdown
Исходник Постоянная ссылка Обычный вид История

2020-10-16 02:29:48 +03:00
---
type: post
title: "Tip 83 - Adding an item to a Azure Storage Table"
excerpt: "Learn how to add an item to a Azure Storage Table"
2020-12-03 11:19:03 +03:00
tags: [Storage]
2020-10-16 02:29:48 +03:00
date: 2018-01-22 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
### Adding an item to 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)
* [Today - 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)
* [Updating an item from a Azure Storage Table](https://microsoft.github.io/AzureTipsAndTricks/blog/tip85.html)
Today, we'll be taking a look at adding an item to the Azure Storage Table that we were working with yesterday.
2020-10-16 02:29:48 +03:00
As a refresher, Azure Storage Blobs can store any type of text or binary data, such as a document, media file, or application installer. Blob storage is also referred to as object storage.
#### Getting Started
Open the C# Console application that we were working with [previously](https://microsoft.github.io/AzureTipsAndTricks/blog/tip82.html) and let's add a folder called **Entities** and add a class named **Thanks**.
Copy the following code into your new class:
```csharp
using Azure;
using Azure.Data.Tables;
2020-10-16 02:29:48 +03:00
using System;
namespace TipsAndTrickSampleTest.Entities
2020-10-16 02:29:48 +03:00
{
class Thanks : ITableEntity
2020-10-16 02:29:48 +03:00
{
public string Name { get; set; }
public DateTime Date { get; set; }
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public DateTimeOffset? Timestamp { get; set; }
public ETag ETag { get; set; }
2020-10-16 02:29:48 +03:00
public Thanks(string name, DateTime date)
{
Name = name;
Date = date;
PartitionKey = "ThanksApp";
RowKey = name;
}
public Thanks()
{
}
}
}
```
This entity use **ITableEntity** as base which will make it easier to work with Azure Storage Tables. We are going to create two fields in our table named **Name** and **Date**. We'll pass in the Name we want to use via a string and provide the current Date for the Date property.
2020-10-16 02:29:48 +03:00
Heading back over to our `Program.cs` file. We'll now add in a helper method to create the item in the table.
2020-10-16 02:29:48 +03:00
```csharp
static void CreateMessage(TableClient table, Thanks message)
2020-10-16 02:29:48 +03:00
{
table.AddEntity(message);
2020-10-16 02:29:48 +03:00
}
```
This will take advantage of our **Thanks** class and we'll pass in the message along with the date in the **Main** method.
2020-10-16 02:29:48 +03:00
The **Main** method inside of the `Program.cs` file just needs to call the method as shown below:
```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 this line
CreateMessage(table, new Thanks("I am thankful for the time with my family", DateTime.Now));
//added this line
2020-10-16 02:29:48 +03:00
Console.ReadKey();
}
```
If we run the program now, then it will add our message along with the current DateTime to our Azure Storage Table called **thankfulfor**. If we want to test it now, then we can use [Azure Storage Explorer](https://microsoft.github.io/AzureTipsAndTricks/blog/tip77.html). If you come back tomorrow, then I'll show you how to do this through code.