当前位置: 首页 > 知识库问答 >
问题:

Azure 表存储 - 最简单的示例

景稳
2023-03-14

每当学习新技术时,我都喜欢写一个尽可能简单的例子。通常这意味着引用最少的控制台应用程序。我一直在尝试编写一个读取和写入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存储资源管理器在我的存储中看到一个表。(仍然不知道如何在manage.windowsazure.com中查看该表。)

但是,如果我尝试插入记录(如前面提到的how-to指南中所述),我会得到一个冲突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中简单地双击表格不会显示表格内容。你必须点击查询。最后一个问题仍然存在。为什么有两个名称空间?

共有2个答案

丁茂
2023-03-14

非常感谢!多年来一直在搜索在开发环境中连接到 Azure 表存储的简单示例。从上面的示例中,我制定了以下代码

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Table;

namespace Bootstrapping
{
  public class Builder
  {

    public void Run()
    {
      CloudStorageAccount storageAccount =
        CloudStorageAccount.Parse("UseDevelopmentStorage=true");
      CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

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

    }

  }
}
卞俊哲
2023-03-14

我能想到的最简单的例子就是这个。你需要重新安装WindowsAzure。存储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存储客户端库2.0包含一个新的简化API。请参阅下面的链接。

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

 类似资料:
  • 问题内容: 使用Javascript最简单的SOAP示例是什么? 为了尽可能有用,答案应该是: 具有功能性(换句话说,实际上有效) 发送至少一个可以在代码中其他位置设置的参数 处理至少一个可以在代码的其他位置读取的结果值 使用大多数现代浏览器版本 在不使用外部库的情况下尽可能清晰明了 问题答案: 这是我可以创建的最简单的JavaScript SOAP客户端。

  • 我必须对Azure表存储进行查询,其中我有以下设置:RowKey、PartitionKey、ThirdColumn RowKey是唯一的,Partitionkey与ThirdColumn相关联,这意味着所有值为“Y”的第三列都将具有分区键“X”。 我必须使用ThirdColumn值获取分区键为X的所有实体。这将不是Performance,因为Y既不是PartitionKey也不是RowKey。 问

  • 问题内容: 因此,我最近一直在尝试了解Socket.io,但是我不是一个非常出色的程序员,并且几乎可以在网络上找到的每个示例(相信我已经花了数小时的时间)都包含使事情变得复杂的额外内容。许多示例都会使我感到困惑,或者连接到一些奇怪的数据库,或者使用coffeescript或大量的JS库将事情弄乱了。 我很乐意看到一个基本的,可以正常运行的示例,其中服务器仅每10秒向客户端发送一条消息,说明现在几点

  • 问题内容: 我有一个对象数组,每个对象都有许多属性。这是通过遍历对象数组获取的一些示例数据: 它的数据很少,所以我想避免使用核心数据。我需要能够持久保存数组,然后再次打开它并能够遍历它。我希望使用一个简单的解决方案,例如NSUserDefaults或NSKeyedArchiver,但是在Swift中我无法使用这种类型的数组(我已经在线阅读文档,论坛和示例了24小时……) 您将如何建议我像上面那样持

  • 问题内容: 有人可以为Java建议一个简单的(很容易理解的)图表库吗? 我只想在程序中添加一个简单的图表,并认为我可以通过学习图书馆来节省一些时间。 问题答案: JFreeChart是Java最好的开源图表软件包之一。

  • 我能够通过Node/Express将文件上传到Azure blob存储,没有任何问题,但我找到了关于如何下载文件的非常少的文档/完整示例。我在教程页面上找到了这个,但是运气不好: 是否有其他人使用node.js/express从Azure blob存储下载文件?您是否使用Azure或其他方法(例如请求)。您能否分享如何从Azure获取文件并将其流式传输到文件夹?