开发者

Empty table query against Azure Table Storage

I'm using Azure Table Storage. When I query a table that is empty with parameters other than PartitionKey and RowKey involved, I get an exception. When I have at least one row, the exception doesn't appear. If I query the empty table with just PartitionKey and RowKey, it is OK.

I certainly do not want to make an extra round trip to test if the table is empty. How do people normally solve this problem? Is there a performant way to quickly check if the table is empty?

I am using the development storage, as I just saw there are reported errors in this scenario with the development storage and the error goes away in production. However, I do not want to开发者_如何学C keep customized code just for development storage, is there a good way to get around this, so I could have the same code running local as well as in production cloud environment?


I've got this around by setting DataServiceContext.IgnoreResoureNotFoundException property to true. Hope this helps others too.


I couldn't ever get the IgnoreResourceNotFoundException to work and punted it. Took a 'naughty' route and just exception trapped for an empty table. Snip below, enjoy...

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜