C# 无法从表存储中获取记录
我有一个表存储表,我想从中获取一些数据。insert和update查询工作正常,但我在尝试选择某些记录时遇到问题。以下是我迄今为止完成的代码:C# 无法从表存储中获取记录,c#,azure,azure-table-storage,C#,Azure,Azure Table Storage,我有一个表存储表,我想从中获取一些数据。insert和update查询工作正常,但我在尝试选择某些记录时遇到问题。以下是我迄今为止完成的代码: class TransactionEntity : TableEntity { public String s{ get; set; } public Int32 r { get; set; } public String e{ get; set; } public String t{ get; set; } pu
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;
}
}
}
类表存储管理器
{
公共布尔AddTransaction(TransactionEntity dto)
{
尝试
{
//创建表客户端。
CloudTableClient tableClient=new CloudTableClient(new System.Uri(ConfigurationManager.AppSettings.Get(“TableStorageURI”))、new StorageCredentials(ConfigurationManager.AppSettings.Get(“TableStorageUser”)、ConfigurationManager.AppSettings.Get(“TableStoragePassword”);
CloudTable=tableClient.GetTableReference(ConfigurationManager.AppSettings.Get(“TableStorageName”);
table.CreateIfNotExists();
TableOperation op=TableOperation.Insert(dto);
表.执行(op);
返回true;
}
捕获(例外情况除外)
{
返回false;
}
}
公共列表检索AllFailedTransactions()
{
尝试
{
CloudTableClient tableClient=new CloudTableClient(new System.Uri(ConfigurationManager.AppSettings.Get(“TableStorageURI”))、new StorageCredentials(ConfigurationManager.AppSettings.Get(“TableStorageUser”)、ConfigurationManager.AppSettings.Get(“TableStoragePassword”);
CloudTable=tableClient.GetTableReference(ConfigurationManager.AppSettings.Get(“TableStorageName”);
TableQuery query=new TableQuery()。其中(“s eq'”+ConfigurationManager.AppSettings.Get(“E”)+”和r lt“+ConfigurationManager.AppSettings.Get(“M”)+”;
Take(ConfigurationTasks.GetResultLength());
return table.ExecuteQuery.ToList();
}
捕获(例外情况除外)
{
返回null;
}
}
公共布尔值UpdateTransactionStatus(TransactionEntity dto)
{
尝试
{
CloudTableClient tableClient=new CloudTableClient(new System.Uri(ConfigurationManager.AppSettings.Get(“TableStorageURI”))、new StorageCredentials(ConfigurationManager.AppSettings.Get(“TableStorageUser”)、ConfigurationManager.AppSettings.Get(“TableStoragePassword”);
CloudTable=tableClient.GetTableReference(ConfigurationManager.AppSettings.Get(“TableStorageName”);
dto.ETag=“*”;
TableOperation op=TableOperation.Replace(dto);
表.执行(op);
返回true;
}
捕获(例外情况除外)
{
返回false;
}
}
}
此外,我还更改了变量名,如果您在阅读变量名时遇到一些麻烦,我深表歉意。我认为继承TableEntity的类中可能缺少默认/无参数构造函数。从表存储接收对象时,非常需要无参数构造函数来反序列化对象 因此,请将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;
}
}
尽管如此,我仍然相信,如果您能够分享更多关于运行代码时遇到的任何异常的详细信息,那将是非常棒的。
有关使用表存储的更好解释,请参阅。您说您“有问题”-但请您更准确一点好吗?到底发生了什么?我注意到您当前正在吞咽所有异常-我将首先删除那些try/catch块,以便异常只会向上传播。。。或者至少在整个过程中记录异常。这样,如果引发异常,您将获得有关错误的更多信息。请编辑您的问题并包括
ConfigurationManager.AppSettings.get(“E”)
、ConfigurationManager.AppSettings.get(“M”)
和ConfigurationTasks.GetResultLength()
的值。我认为其中一个存在价值观问题。