无法从表存储中获取记录 [英] Unable to Get records from Table Storage

查看:66
本文介绍了无法从表存储中获取记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表存储表,我想从中存储一些数据.插入和更新查询工作正常,但是当我尝试选择一些记录时遇到了问题.下面是到目前为止我完成的代码:

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;
    }
}

用于管理表格的代码:

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.

推荐答案

我相信您的继承TableEntity的类中可能缺少默认/无参数的构造函数.从TableStorage接收到该对象时,非常需要无参数的构造函数来反序列化该对象.

I believe that there might be a default/parameter-less constructor missing from your class inheriting the TableEntity. The parameter-less constructor is very much needed to deserialize the object when recieved from the TableStorage.

因此,将您的TableEntity代码更改为以下内容:

So change your TableEntity code to something like:

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.

这篇关于无法从表存储中获取记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆