2013-06-05 25 views
11

Bất cứ khi nào học công nghệ mới, tôi muốn viết ví dụ đơn giản nhất có thể. Thông thường, điều này có nghĩa là ứng dụng bảng điều khiển có số lượng tham chiếu ít nhất. Tôi đã cố gắng, với rất ít thành công, để viết một ứng dụng đọc và ghi vào lưu trữ bảng Azure. Tôi đã sử dụng this hướng dẫn cách làm cơ sở, nhưng cố gắng thực hiện mọi thứ theo phương pháp Chính. Cách tiếp cận tương tự đã làm việc tốt với bộ nhớ blob, nhưng bộ nhớ bảng đang gây rắc rối.Lưu trữ bảng Azure - Ví dụ đơn giản nhất có thể

Tôi có thể tạo bảng bằng mã này.

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

Sau khi chạy mã này tôi có thể thấy bảng trong bộ nhớ của mình bằng cách sử dụng Azure Storage Explorer. (Vẫn chưa tìm ra cách để xem bảng trong manage.windowsazure.com.)

Tuy nhiên, nếu tôi cố chèn bản ghi (như được mô tả trong hướng dẫn cách thực hiện trước đây), tôi nhận được xung đột 409 EntityAlreadyExists. Azure Storage Explorer không hiển thị bất kỳ bản ghi nào trong bảng của tôi.

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

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

Ngoài ra, tôi bị bối rối bởi hai không gian tên trùng nhau. Cả Microsoft.WindowsAzure.Storage.Table và Microsoft.WindowsAzure.StorageClient đều chứa e.g. một lớp CloudTableClient. Tại sao có hai không gian tên máy khách và tôi phải sử dụng không gian tên nào?

EDIT Tắt bản ghi tồn tại. Đơn giản chỉ cần kích đúp vào bảng trong Azure Table Explorer sẽ không hiển thị nội dung của bảng. Bạn phải bấm Truy vấn. Câu hỏi cuối cùng vẫn đứng vững. Tại sao hai không gian tên?

Trả lời

15

Mẫu đơn giản nhất tôi có thể nghĩ đến là đây. Bạn cần 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() { } 
} 

Câu trả lời cho câu hỏi giây, tại sao có hai không gian tên cung cấp nhiều hay ít cùng API, Azure Storage Client Library 2.0 chứa API đơn giản mới. Xem liên kết bên dưới.

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

+0

Câu trả lời rất hữu ích. Để sử dụng với bộ mô phỏng Azure lưu trữ cục bộ, thay thế chuỗi kết nối dài bằng "UseDevelopmentStorage = true". –

+0

liên kết cuối cùng bị hỏng – Serge

+0

@Serge Có vẻ như nó đã bị xóa khỏi MSDN. – Rubio

0

Cảm ơn rất nhiều vì điều này! Bạn đang tìm kiếm độ tuổi cho một ví dụ đơn giản về kết nối với Bộ nhớ bảng Azure trong môi trường phát triển của bạn. Từ ví dụ của bạn ở trên, tôi đã xây dựng mã bên dưới:

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

Bài viết này rất hữu ích: https://www.simple-talk.com/cloud/cloud-data/an-introduction-to-windows-azure-table-storage/ – Garth

Các vấn đề liên quan