3

我正在使用 Azure 表存储。当我查询包含 PartitionKey 和 RowKey 以外的参数的空表时,出现异常。当我至少有一行时,不会出现异常。如果我只用 PartitionKey 和 RowKey 查询空表,就可以了。

我当然不想做额外的往返来测试桌子是否是空的。人们通常如何解决这个问题?有没有一种快速检查表是否为空的高效方法?

我正在使用开发存储,因为我刚刚看到在这种情况下使用开发存储报告了错误,并且错误在生产中消失了。但是,我不想只为开发存储保留自定义代码,有没有一种很好的方法来解决这个问题,所以我可以在本地以及生产云环境中运行相同的代码?

4

2 回答 2

8

我通过将DataServiceContext.IgnoreResoureNotFoundException属性设置为 true 来解决这个问题。希望这对其他人也有帮助。

于 2011-01-03T20:23:27.473 回答
0

我永远无法让 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();

于 2015-02-27T22:00:48.740 回答