2016-08-03 153 views
7

所有記錄使用此代碼塊獲取從蔚藍的表存儲

try 
{ 
    StorageCredentials creds = new StorageCredentials(accountName, accountKey); 
    CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true); 

    CloudTableClient client = account.CreateCloudTableClient(); 
    CloudTable table = client.GetTableReference("serviceAlerts"); 

    TableOperation retrieveOperation = TableOperation.Retrieve<ServiceAlertsEntity>("ServiceAlerts", "b9ccd839-dd99-4358-b90f-46781b87f933"); 

    TableResult query = table.Execute(retrieveOperation); 

    if (query.Result != null) 
    { 
     outline = outline + ((ServiceAlertsEntity) query.Result).alertMessage + " * "; 
    } 
    else 
    { 
     Console.WriteLine("No Alerts"); 
    } 
} 
catch (Exception ex) 
{ 
    Console.WriteLine(ex); 
} 

我能夠檢索與在檢索中提到的分區和rowkey單個記錄。

有沒有一種方法可以獲取存儲在ServiceAlerts分區中的所有記錄?

我已經嘗試了第二個參數

TableOperation retrieveOperation = TableOperation.Retrieve<ServiceAlertsEntity>(
     "ServiceAlerts","b9ccd839-dd99-4358-b90f-46781b87f933"); 

一個通配符(*),但它不返回任何東西。

回答

4

你需要指定一個TableQuery,這會給你所有的實體,或者你可以指定一個TableQuery.GenerateFilterCondition來過濾行。

TableQuery<ServiceAlertsEntity> query = new TableQuery<ServiceAlertsEntity>(); 

foreach (ServiceAlertsEntity entity in table.ExecuteQuery(query)) 
{ 
    Console.WriteLine("{0}, {1}\t{2}\t{3}", entity.PartitionKey, entity.RowKey, 
         entity.Field1, entity.Field2); 
} 
+0

table.ExecuteQuery返回一個IEnumerable,這意味着它會在多個分區服務器將所有數據。這種方法不好 –

0

如果你需要記錄的進一步控制返回,您可以使用ExecuteQuerySegmentedAsync在一次檢索數據的頁面(約1000條記錄)。

var alerts = new List<ServiceAlertsEntity>(); 

    var query = new TableQuery<ServiceAlertsEntity>(); 
    TableContinuationToken continuationToken = null; 
    do 
    { 
     var page = await table.ExecuteQuerySegmentedAsync(query, continuationToken); 
     continuationToken = page.ContinuationToken; 
     alerts.AddRange(page.Results); 
    } 
    while (continuationToken != null); 

或者,如果您需要限制結果,例如,通過分區鍵,可以通過在上述代碼中向查詢中添加Where子句來添加過濾條件。

var pk = "abc"; 
    var filterPk = TableQuery.GenerateFilterCondition(
     nameof(ServiceAlertsEntity.PartitionKey), 
     QueryComparisons.Equal, pk); 

    var query = new TableQuery<ServiceAlertsEntity>().Where(filterPk); 

MS Azure reference