0

在我的表执行查询期间,我偶尔会遇到以下错误

System.InvalidOperationException: '已经有一个打开的 DataReader 与此命令关联,必须先关闭。'

private CloudTable table;

    public AzureTableManager(string _CloudTableName)
    {
        if (string.IsNullOrEmpty(_CloudTableName))
        {
            throw new ArgumentNullException("Table", "Table Name can't be empty");
        }
        try
        {
            string ConnectionString = PrimariusOMS.Properties.Settings.Default.AzureTableStorage.ToString();
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            table = tableClient.GetTableReference(_CloudTableName);
            table.CreateIfNotExists();
        }
        catch (StorageException StorageExceptionObj)
        {
            throw StorageExceptionObj;
        }
        catch (Exception ExceptionObj)
        {
            throw ExceptionObj;
        }
    }

    public List<T> RetrieveEntity<T>(string TenantName, string Query = null) where T : TableEntity, new()
    {
TableQuery<T> DataTableQuery = new TableQuery<T>();
            if (!String.IsNullOrEmpty(Query))
            {
                Query += " and PartitionKey eq '" + TenantName + "'";
            }
            else { Query = "PartitionKey eq '" + TenantName + "'"; }

            DataTableQuery = new TableQuery<T>().Where(Query);
            IEnumerable<T> IDataList = table.ExecuteQuery(DataTableQuery);
            List<T> DataList = new List<T>();
            foreach (var singleData in IDataList)
                DataList.Add(singleData);
            return DataList;
}

此处返回错误:

IEnumerable<T> IDataList = table.ExecuteQuery(DataTableQuery);

这是我的示例调用:

List<AzureTableManager.DTTConfig> dtt = TableManagerObj.RetrieveEntity<AzureTableManager.DTTConfig>(tenantID, "RowKey eq 'Mass WhatIf'");

在 SQL 连接中,通过使用块或确保阅读器已关闭来克服此问题。我这里怎么处理?

4

1 回答 1

1

您可以尝试将您的参考更新到最新版本的WindowsAzure.Storage.

在新版本中,CloudTable该类不再包含ExecuteQuery方法。相反,它有ExecuteQueryAsyncExecuteQuerySegmentedAsync方法。

此代码适用于我获取数据:

public static async Task<List<T>> ExecuteQueryToListAsync<T>(this CloudTable table, TableQuery<T> query) where T : ITableEntity, new()
{
    var result = new List<T>();

    TableContinuationToken token = null;
    do
    {
        TableQuerySegment<T> seg = await table.ExecuteQuerySegmentedAsync(query, token);
        token = seg.ContinuationToken;
        result.AddRange(seg);
    }
    while (token != null);

    return result;
}
于 2020-04-06T06:09:18.537 回答