Azure表存储-最简单的示例 [英] Azure table storage - Simplest possible example

查看:85
本文介绍了Azure表存储-最简单的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当学习新技术时,我都会写一个最简单的例子.通常,这意味着引用数量最少的控制台应用程序.我一直在尝试写一个可以读写Azure表存储的应用程序,但收效甚微.我已经使用 -以指导为基础,但是尝试使用Main方法做所有事情.类似的方法在Blob存储中效果很好,但是表存储却带来​​了麻烦.

我能够用此代码创建一个表.

static void Main(string[] args)
{
    Microsoft.WindowsAzure.Storage.Table.CloudTableClient tableClient =
    new Microsoft.WindowsAzure.Storage.Table.CloudTableClient(
        new Uri("http://mystorage.table.core.windows.net/"),
    new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("[somename]", "[somekey]"));

    CloudTable table = tableClient.GetTableReference("people");
    table.CreateIfNotExists();
}

运行此代码后,我可以使用 Azure Storage Explorer 在存储中看到一个表. (仍然没有弄清楚如何在manage.windowsazure.com中查看表格.)

但是,如果我尝试插入记录(如前面提到的操作指南中所述),则会发生冲突409 EntityAlreadyExists. Azure Storage Explorer在我的表中不显示任何记录.

CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
customer1.Email = "Walter@contoso.com";
customer1.PhoneNumber = "425-555-0101";

TableOperation insertOperation = TableOperation.Insert(customer1);
table.Execute(insertOperation);

此外,我对两个重叠的命名空间感到困惑. Microsoft.WindowsAzure.Storage.Table和Microsoft.WindowsAzure.StorageClient都包含例如一个CloudTableClient类.为什么会有两个客户端名称空间,我应该使用哪一个?

编辑,结果表明该记录确实存在.只需在Azure Table Explorer中双击表就不会显示表内容.您必须单击查询.最后一个问题仍然存在.为什么要使用两个命名空间?

解决方案

我能想到的最简单的示例就是这个.您需要使用NuGet WindowsAzure.Storage 2.0.

static void Main(string[] args)
{
  try
  {
     CloudStorageAccount storageAccount =
        CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=<your_storage_name>;AccountKey=<your_account_key>");
     CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

     CloudTable table = tableClient.GetTableReference("people");
     table.CreateIfNotExists();

     CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
     customer1.Email = "Walter@contoso.com";
     customer1.PhoneNumber = "425-555-0101";

     // Create the TableOperation that inserts the customer entity.
     var insertOperation = TableOperation.Insert(customer1);

     // Execute the insert operation.
     table.Execute(insertOperation);

     // Read storage
     TableQuery<CustomerEntity> query =
        new TableQuery<CustomerEntity>()
           .Where(TableQuery.GenerateFilterCondition("PartitionKey",
               QueryComparisons.Equal, "Harp"));
     var list = table.ExecuteQuery(query).ToList();
   }
   catch (StorageException ex)
   {
       // Exception handling here.
   }
}

public class CustomerEntity : TableEntity
{
    public string Email { get; set; }
    public string PhoneNumber { get; set; }

    public CustomerEntity(string lastName, string firstName)
    {
        PartitionKey = lastName;
        RowKey = firstName;
    }

    public CustomerEntity() { }
}

秒问题的答案,为什么有两个命名空间提供或多或少相同的API,Azure Storage Client Library 2.0包含一个新的简化的API.请参阅下面的链接.

.NET(2.0版)的存储客户端库中的新增功能

Whenever learning new technologies I like to write the simplest possible example. Usually this means a console app with the least number of references. I've been trying, with little success, to write an app that reads and writes to Azure table storage. I've used this how-to guide as a basis, but try to do everything in the Main method. Similar approach worked well with the blob storage, but the table storage is giving trouble.

I was able to create a table with this code.

static void Main(string[] args)
{
    Microsoft.WindowsAzure.Storage.Table.CloudTableClient tableClient =
    new Microsoft.WindowsAzure.Storage.Table.CloudTableClient(
        new Uri("http://mystorage.table.core.windows.net/"),
    new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("[somename]", "[somekey]"));

    CloudTable table = tableClient.GetTableReference("people");
    table.CreateIfNotExists();
}

After running this code I could see a table in my storage using Azure Storage Explorer. (Still haven't figured out how to see the table in manage.windowsazure.com.)

However, if I try to insert records (as described in the how-to guide mentioned before), I get a conflict 409 EntityAlreadyExists. Azure Storage Explorer doesn't show any records in my table.

CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
customer1.Email = "Walter@contoso.com";
customer1.PhoneNumber = "425-555-0101";

TableOperation insertOperation = TableOperation.Insert(customer1);
table.Execute(insertOperation);

Also, I'm baffled by the two overlapping namespaces. Both Microsoft.WindowsAzure.Storage.Table and Microsoft.WindowsAzure.StorageClient contain e.g. a CloudTableClient class. Why are there two client namespaces and which one am I supposed to use?

EDIT Turns out the record does exist. Simply double-clicking the table in Azure Table Explorer doesn't show the table contents. You have to click Query. The last question still stands. Why the two namespaces?

解决方案

The simplest sample I could think of is this. You need to NuGet WindowsAzure.Storage 2.0.

static void Main(string[] args)
{
  try
  {
     CloudStorageAccount storageAccount =
        CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=<your_storage_name>;AccountKey=<your_account_key>");
     CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

     CloudTable table = tableClient.GetTableReference("people");
     table.CreateIfNotExists();

     CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
     customer1.Email = "Walter@contoso.com";
     customer1.PhoneNumber = "425-555-0101";

     // Create the TableOperation that inserts the customer entity.
     var insertOperation = TableOperation.Insert(customer1);

     // Execute the insert operation.
     table.Execute(insertOperation);

     // Read storage
     TableQuery<CustomerEntity> query =
        new TableQuery<CustomerEntity>()
           .Where(TableQuery.GenerateFilterCondition("PartitionKey",
               QueryComparisons.Equal, "Harp"));
     var list = table.ExecuteQuery(query).ToList();
   }
   catch (StorageException ex)
   {
       // Exception handling here.
   }
}

public class CustomerEntity : TableEntity
{
    public string Email { get; set; }
    public string PhoneNumber { get; set; }

    public CustomerEntity(string lastName, string firstName)
    {
        PartitionKey = lastName;
        RowKey = firstName;
    }

    public CustomerEntity() { }
}

The answer to the seconds question, why are there two namespaces that provided more or less the same APIs, Azure Storage Client Library 2.0 contains a new simplified API. See link below.

What's New in Storage Client Library for .NET (version 2.0)

这篇关于Azure表存储-最简单的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆