pithhelmet pithhelmet - 4 months ago 43
C# Question

Get all records from azure table storage

Using this code block

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


I am able to retrieve the single record with the partition and rowkey mentioned in the retrieve.

Is there a way I can get all the records that are stored in the partition of ServiceAlerts?

I have tried a wildcard (*) for the second parameter

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


but it does not return anything

Answer

You need to specify a TableQuery, this will give you all entities or you can specify a TableQuery.GenerateFilterCondition to filter the rows.

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