2013-06-05 15 views
11

Zawsze, gdy uczę się nowych technologii, lubię pisać najprostszy możliwy przykład. Zwykle oznacza to aplikację konsolową z najmniejszą ilością odniesień. Próbowałem, z niewielkim powodzeniem, napisać aplikację, która czyta i zapisuje do magazynu tabeli Azure. Użyłem instrukcji obsługi jako podstawy, ale staram się robić wszystko w metodzie głównej. Podobne podejście działało dobrze z pamięcią typu blob, ale pamięć podręczna stwarza problemy.Składowanie tablic Azure - najprostszy możliwy przykład:

Udało mi się utworzyć tabelę z tym kodem.

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

Po uruchomieniu tego kodu mogłem zobaczyć tabelę w mojej pamięci za pomocą Azure Storage Explorer. (Nadal nie wiem, jak wyświetlić tabelę w manage.windowsazure.com.)

Jednak jeśli spróbuję wstawić rekordy (zgodnie z opisem w powyższym przewodniku), pojawia się konflikt 409 EntityAlreadyExists. Azure Storage Explorer nie wyświetla żadnych rekordów w mojej tabeli.

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

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

Co więcej, jestem zaskoczony dwoma nakładającymi się przestrzeniami nazw. Zarówno Microsoft.WindowsAzure.Storage.Table, jak i Microsoft.WindowsAzure.StorageClient zawierają np. klasa CloudTableClient. Dlaczego istnieją dwa przestrzenie nazw klienta i którego mam używać?

EDIT WYDŁUŻA rekord istnieje. Po dwukrotnym kliknięciu tabeli w Eksploratorze tabel Azure nie jest wyświetlana zawartość tabeli. Musisz kliknąć Zapytanie. Ostatnie pytanie nadal pozostaje. Dlaczego te dwie przestrzenie nazw?

Odpowiedz

15

Najprostszą próbką, o której mogę pomyśleć, jest to. Potrzebujesz 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 = "[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() { } 
} 

Odpowiedź na pytanie sekund, dlaczego tam są dwie przestrzenie nazw, które przewidziane mniej więcej te same interfejsy API, Azure Storage Client Library 2.0 zawiera nowy uproszczony interfejs API. Zobacz link poniżej.

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

+0

Bardzo przydatna odpowiedź. Aby użyć go z lokalnym emulatorem platformy Azure, zamień długi ciąg połączenia na "UseDevelopmentStorage = true". –

+0

ostatni link jest zepsuty – Serge

+0

@Serge Wygląda na to, że został usunięty z MSDN. – Rubio

0

Dzięki tak dużo za to! Szukałem od wieków prostego przykładu połączenia z platformą Azure Table Storage w twoim środowisku programistycznym. Z powyższych przykładów sformułowałem poniższy kod:

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

Ten artykuł również był bardzo pomocny: https://www.simple-talk.com/cloud/cloud-data/an-introduction-to-windows-azure-table-storage/ – Garth

Powiązane problemy