Query an empty table for Azure table storage

I am using Azure Table Storage. When I query a table that is empty, with parameters other than PartitionKey and RowKey, I get an exception. When I have at least one line, an exception does not appear. If I query an empty table using only PartitionKey and RowKey, this is normal.

Of course, I do not want to do an extra round to check if there is a table. How do people usually solve this problem? Is there a way to quickly check if a table is empty?

I use the repository for development, as I just saw that there were errors in the development repository in this scenario, and the error disappears during production. However, I don’t want to save the customized code for the development repository only, is there a good way around this, so I could have the same code as the on-premises as well as in a working cloud?

+3
source share
2 answers

I have this by setting the DataServiceContext.IgnoreResoureNotFoundException property to true. Hope this helps others too.

+8
source

IgnoreResourceNotFoundException . "" , . , ...

CloudTableClient _tableClient = OurStorageAccount.CreateCloudTableClient();
CloudTable _table = _tableClient.GetTableReference( "customers" );

TableQuery<CustomerEntity> _query = new TableQuery<CustomerEntity>();
var _result = _table.ExecuteQuery( _query );

StringBuilder _sb = new StringBuilder(1024);
try
{ 
	_sb.AppendLine("Begin listing customers....<br/>");
	foreach ( CustomerEntity _customer in _result )
	{
		_sb.AppendFormat( "{0} {1} - {2} - {3}<br/>", _customer.PartitionKey, _customer.RowKey, _customer.Email, _customer.PhoneNumber );
	}
	_sb.AppendLine("End listing customers....<br/>");
}
catch ( System.NullReferenceException _nullEx )
{ 
	_sb.Append( System.DateTime.Now.ToString() );
	_sb.AppendLine( ": no customer entries found<br/>" );
	System.Diagnostics.Debug.WriteLine( _nullEx.ToString());
	// _sb.AppendLine( _nullEx.ToString() );
}
catch (Exception _ex)
{
	_sb.AppendLine("unkown exception thrown, good luck<br/>");
	_sb.AppendLine( _ex.ToString() );
}

Label_Output.Text = _sb.ToString();
Hide result
0

Source: https://habr.com/ru/post/1783247/


All Articles