2013-06-05 13 views
11

新しいテクノロジーを学ぶたびに、私は可能な限り単純な例を書いています。通常これは、参照数が最も少ないコンソールアプリケーションを意味します。私はAzureテーブルストレージを読み書きするアプリケーションを書くのにはほとんど成功していない。私はthisハウツーガイドを基礎として使用しましたが、Mainメソッドですべてを実行しようとします。同様のアプローチはBLOBストレージでうまくいきましたが、テーブルストレージは問題を引き起こしています。Azureテーブルストレージ - 最も単純な可能性のある例

このコードでテーブルを作成することができました。

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ストレージエクスプローラは、テーブルにレコードを表示しません。

CustomerEntity customer1 = new CustomerEntity("Harp", "Walter"); 
customer1.Email = "[email protected]"; 
customer1.PhoneNumber = "425-555-0101"; 

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

また、2つの重複する名前空間に困惑しています。 Microsoft.WindowsAzure.Storage.TableとMicrosoft.WindowsAzure.StorageClientの両方には、 CloudTableClientクラス。なぜ2つのクライアントの名前空間があり、どちらを使うのですか?

EDITレコードが存在することが分かります。 Azure Table Explorerでテーブルをダブルクリックするだけでは、テーブルの内容は表示されません。クエリをクリックする必要があります。最後の質問はまだ立っています。なぜ2つの名前空間?

答えて

15

私が考えることができる最も簡単なサンプルはこれです。あなたはWindowsAzure.Storage 2.0をNuGetする必要があります。多かれ少なかれ同じAPIを提供する2つのネームスペースがある理由

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 = "[email protected]"; 
    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() { } 
} 

秒の質問への答えは、Azureストレージクライアントライブラリ2.0は、新しい単純化されたAPIが含まれています。下のリンクを参照してください。このため

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

+0

非常に便利な答えです。ローカルのAzure Storageエミュレータで使用する場合は、長い接続文字列を "UseDevelopmentStorage = true"に置き換えてください。 –

+0

最後のリンクが壊れています – Serge

+0

@Serge MSDNから削除されているようです。 – Rubio

0

ありがとうを! 開発環境でAzure Table Storageに接続する簡単な例を探してきました。上の例から、私は以下のコードを公式化しました:

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(); 

    } 

    } 
} 
+0

この記事も非常に参考になりました:https://www.simple-talk.com/cloud/cloud-data/an-introduction-to-windows-azure-table-storage/ – Garth

関連する問題