列出Azure存储帐户.NET Core中的所有现有表

时间:2018-07-13 13:28:50

标签: c# azure asp.net-core azure-storage azure-table-storage

如何获取在Azure存储帐户中创建的所有现有表?

您以前是这样做的

CloudTableClient tableClient;
IEnumerable<CloudTable> AllTables = tableClient.ListTables();

但是,似乎无法在ListTables的实例上调用CloudTableClient方法。我正在使用Microsoft.WindowsAzure.Storage Version 9.3.0;

  

编辑

这是.NET CORE的

1 个答案:

答案 0 :(得分:2)

  

如果您要查找.NET核心答案,请直接转到下面的EDIT段落

好象您没有创建tableClient的实例

    CloudTableClient tableClient = new CloudTableClient("YOUR CONNECTION STRING");
    var AllTables = tableClient.ListTables();          
    if(AllTables != null)
    {
        foreach (var table in AllTables)
        {
            // table.Name is your property
        } 
    }

该方法在Microsoft文档上

https://docs.microsoft.com/en-us/dotnet/api/microsoft.windowsazure.storage.table.cloudtableclient.listtables?view=azure-dotnet#Microsoft_WindowsAzure_Storage_Table_CloudTableClient_ListTables_System_String_Microsoft_WindowsAzure_Storage_Table_TableRequestOptions_Microsoft_WindowsAzure_Storage_OperationContext_

  

编辑

此OP刚刚说他们正在使用.NET CORE

.NET Core尚未包括API的同步实现

ListTables没有方法,因此您必须使用ListTablesSegmentedAsync并将null作为continuationToken参数传递。这将一直循环直到获取所有表

https://docs.microsoft.com/en-us/dotnet/api/microsoft.windowsazure.storage.table.cloudtableclient.listtablessegmentedasync?view=azure-dotnet#Microsoft_WindowsAzure_Storage_Table_CloudTableClient_ListTablesSegmentedAsync_Microsoft_WindowsAzure_Storage_Table_TableContinuationToken_

更新示例代码

这是列出所有表的伪代码

CloudTableClient tableClient = new CloudTableClient("YOUR CONNECTION STRING");
TableContinuationToken continuationToken = null;
var allTables = new List<CloudTable>();
do
{
  var listingResult = await tableClient.ListTablesSegmentedAsync(continuationToken);
  var tables = listingResult.Result.ToList();
  var continuationToken = listingResult.ContinuationToken;
  //Add the tables to your allTables
}
while (continuationToken != null);