如何将数据插入 azure table 存储

How to insert data into azure table storage

我正在寻找一个简单的 C# 程序来将数据插入到 Azure blob table 存储中。 有人能帮忙吗?

请告诉我以下代码有什么问题?(代码不会抛出任何错误,但不会创建任何 table / 插入数据)

  
 using System;
using System.Threading.Tasks;
using Azure.Data.Tables;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using TableEntity = Microsoft.WindowsAzure.Storage.Table.TableEntity;
using TableClientConfiguration = Microsoft.Azure.Cosmos.Table.TableClientConfiguration;
public class CustomerEntity : TableEntity
{
    public CustomerEntity(string lastName, string firstName)
    {
        this.PartitionKey = lastName;
        this.RowKey = firstName;
    }
    public CustomerEntity() { } // the parameter-less constructor must be provided
    public string Email { get; set; }
    public string PhoneNumber { get; set; }
}
class Program
{
    static void Main(string[] args) {
        
        var tableName = "TestTempTable";        
        var storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=**********;AccountKey=*******/****==;EndpointSuffix=core.windows.net";        
        try
        {
            Console.WriteLine("START");            
            var storageAccount = CloudStorageAccount.Parse(storageConnectionString);            
            var tableClient = storageAccount.CreateCloudTableClient();
            var table = tableClient.GetTableReference(tableName);
            table.CreateIfNotExistsAsync();            
            Console.WriteLine($"CloudTable name is : {tableClient}");
            // Create a new customer entity.
            CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
            customer1.Email = "xyz@xyz.com";
            customer1.PhoneNumber = "1234568";            
            table.ExecuteAsync(TableOperation.Insert(customer1));            
            Console.WriteLine("Records Inserted");
            Console.WriteLine("END");
        }
        catch (Exception e)
        {
            Console.WriteLine("Encountered Exception - "+e);
        }
    }
}   

谢谢, 保罗.

你的程序没有抛出任何错误并且没有插入实体的原因是你没有等待异步操作并且你的程序在成功之前终止。这就是为什么诸如“插入的记录”之类的行像正常程序执行一样运行的原因。考虑在您的程序中的这两行中使用 await

await table.CreateIfNotExistsAsync();

await table.ExecuteAsync(TableOperation.Insert(customer1));