【发布时间】:2019-04-10 09:08:52
【问题描述】:
我有一个表存储表,我想从中获取一些数据。插入和更新查询工作正常,但是当我尝试选择一些记录时遇到问题。以下是我到目前为止所做的代码:
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;
}
}
}
另外,我已经更改了变量名,如果你们在阅读它们时遇到了一些麻烦,我们深表歉意。
【问题讨论】:
-
你说你“有问题” - 但请你能更准确一点吗?到底发生了什么?我注意到您目前正在吞下所有异常 - 我将通过删除那些 try/catch 块来开始,以便异常向上传播......或者至少在此过程中记录异常。这样,如果抛出异常,您将获得更多有关问题所在的信息。
-
请编辑您的问题并包含
ConfigurationManager.AppSettings.Get("E")、ConfigurationManager.AppSettings.Get("M")和ConfigurationTasks.GetResultLength()的值。我认为其中之一的值存在问题。
标签: c# azure azure-table-storage