简体   繁体   English

无法从表存储中获取记录

[英]Unable to Get records from Table Storage

I have a table storage table from which I want to fetch some data.我有一个表存储表,我想从中获取一些数据。 The insert and update queries work fine but I am having issues when I try to select some records.插入和更新查询工作正常,但当我尝试选择一些记录时遇到问题。 Below is the code I have done so far:以下是我到目前为止所做的代码:

class TransactionEntity : TableEntity
{
    public String s{ get; set; }
    public Int32 r { get; set; }
    public String e{ get; set; }
    public String t{ get; set; }
    public String l{ get; set; }
    public TransactionEntity(String id, String s, String e, String t)
    {
        this.r= 0;
        this.s= s;
        this.RowKey = id;
        this.PartitionKey = Guid.NewGuid().ToString();
        this.e= e == null ? "" : e;
        this.t= t== null ? "" : t;
    }
}

The code for managing the Table:管理表的代码:

class TableStorageManager
{
    public Boolean AddTransaction(TransactionEntity dto)
    {
        try
        {
            // Create the table client.
            CloudTableClient tableClient = new CloudTableClient(new System.Uri(ConfigurationManager.AppSettings.Get("TableStorageURI")), new StorageCredentials(ConfigurationManager.AppSettings.Get("TableStorageUser"), ConfigurationManager.AppSettings.Get("TableStoragePassword")));
            CloudTable table = tableClient.GetTableReference(ConfigurationManager.AppSettings.Get("TableStorageName"));
            table.CreateIfNotExists();
            TableOperation op = TableOperation.Insert(dto);    
            table.Execute(op);    
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

    public List<TransactionEntity> RetrieveAllFailedTransactions()
    {
        try
        {
            CloudTableClient tableClient = new CloudTableClient(new System.Uri(ConfigurationManager.AppSettings.Get("TableStorageURI")), new StorageCredentials(ConfigurationManager.AppSettings.Get("TableStorageUser"), ConfigurationManager.AppSettings.Get("TableStoragePassword")));
            CloudTable table = tableClient.GetTableReference(ConfigurationManager.AppSettings.Get("TableStorageName"));         
            TableQuery<TransactionEntity> query = new TableQuery<TransactionEntity>().Where("s eq '" + ConfigurationManager.AppSettings.Get("E") + "' and r lt " + ConfigurationManager.AppSettings.Get("M") + "");
            query.Take(ConfigurationTasks.GetResultLength());  
            return table.ExecuteQuery(query).ToList();
        }
        catch (Exception ex)
        {
            return null;
        }
    }

    public Boolean UpdateTransactionStatus(TransactionEntity dto)
    {
        try
        {                    
            CloudTableClient tableClient = new CloudTableClient(new System.Uri(ConfigurationManager.AppSettings.Get("TableStorageURI")), new StorageCredentials(ConfigurationManager.AppSettings.Get("TableStorageUser"), ConfigurationManager.AppSettings.Get("TableStoragePassword")));                   
            CloudTable table = tableClient.GetTableReference(ConfigurationManager.AppSettings.Get("TableStorageName"));
            dto.ETag = "*";
            TableOperation op = TableOperation.Replace(dto);
            table.Execute(op);    
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
}

Also I have changed the variable names so sorry if you people get a bit of trouble reading them.此外,如果您在阅读它们时遇到一些麻烦,我也更改了变量名称,所以很抱歉。

I believe that there might be a default/parameter-less constructor missing from your class inheriting the TableEntity.我相信继承 TableEntity 的类中可能缺少默认/无参数构造函数。 The parameter-less constructor is very much needed to deserialize the object when recieved from the TableStorage.当从 TableStorage 接收到对象时,非常需要无参数构造函数来反序列化对象。

So change your TableEntity code to something like:因此,将您的 TableEntity 代码更改为:

class TransactionEntity : TableEntity
{
    public String s{ get; set; }
    public Int32 r { get; set; }
    public String e{ get; set; }
    public String t{ get; set; }
    public String l{ get; set; }
    public TransactionEntity(){//do nothing}
    public TransactionEntity(String id, String s, String e, String t)
    {
        this.r= 0;
        this.s= s;
        this.RowKey = id;
        this.PartitionKey = Guid.NewGuid().ToString();
        this.e= e == null ? "" : e;
        this.t= t== null ? "" : t;
    }
}

Although still I believe it would be great if you can share a bit more details about whatever exception you are getting on running your code.尽管我仍然相信,如果您可以分享更多有关运行代码时遇到的任何异常的详细信息,那将会很棒。 Also refer to this link for better explanation of working with TableStorage.另请参阅此链接以更好地解释如何使用 TableStorage。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM