【问题标题】:Azure SDK for TableStorage with .NET 4.5Azure SDK for TableStorage with .NET 4.5
【发布时间】:2012-11-21 21:13:02
【问题描述】:

我正在寻找一些关于如何使用 Azure SDK 在 Azure 表存储中存储和检索数据的示例或文档。

我使用 C# 和 .NET 4.5 框架。

我在这里找到了https://www.windowsazure.com/en-us/develop/net/how-to-guides/table-services/ 文档。

部分:

// Create the CloudTable object that represents the "people" table.
CloudTable table = tableClient.GetTableReference("people");

// Create a new customer entity.
CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
customer1.Email = "Walter@contoso.com";
customer1.PhoneNumber = "425-555-0101";

// Create the TableOperation that inserts the customer entity.
TableOperation insertOperation = TableOperation.Insert(customer1);

// Execute the insert operation.
table.Execute(insertOperation);

不再可用。

有人知道如何使用 .NET 4.5 做到这一点吗?

最好的问候

【问题讨论】:

    标签: c# azure azure-storage azure-table-storage


    【解决方案1】:

    此代码运行良好。虽然文档中提到了 2.0 版,但这指向的是存储 SDK 的版本,而不是 .NET 的版本。

    编辑:

    为了获得 GetTableReference 方法,您需要执行以下操作:

    • 参考 Microsoft.WindowsAzure.Storage.dll 版本 2.0.0.0 或更高版本(通过 NuGet:Install-Package WindowsAzure.Storage
    • 添加以下命名空间:

    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Auth;
    using Microsoft.WindowsAzure.Storage.Table;
    
    • 初始化存储帐户和表客户端。

    var account = new CloudStorageAccount(...);
    var tableClient = account.CreateCloudTableClient();
    

    【讨论】:

    • tableClient 或 CloudTable 类肯定没有 getTableReference() 方法!您使用的是哪个 .NET 版本?
    • 几天前我写了一篇博文,介绍了一些关于使用存储客户端库 2.0 的简单示例,您可以在此处阅读:gauravmantri.com/2012/11/17/…。 HTH。
    【解决方案2】:

    这是我的表存储管理器类,我使用的是 .NET 4.5

    public class TableStorageManager
    {
        private CloudTableClient TableClient;
        private CloudTable Table;
    
        #region Sigleton implementation
    
        public TableStorageManager(string tablename)
        {
            TableClient = StorageFactory.GetCloudStorageAccount().CreateCloudTableClient();
            Table = TableClient.GetTableReference(tablename);
            //var ctx = TableClient.GetTableServiceContext();
    
            Table.CreateIfNotExists();
        }
    
        #endregion
    
        public void InsertAnyEntity<T>(T entity)
        {
            var translatedEntity = Helper.ConvertToDictionaryEntity<T>(entity);
            InsertEntity<DictionaryEntity>(translatedEntity);
        }
        public void InsertEntity<T>(T entity) where T : ITableEntity
        {
            Table.Execute(TableOperation.InsertOrReplace(entity));
        }
    
        public IEnumerable<T> ExecuteQuery<T>(TableQuery<T> query) where T : class, ITableEntity, new()
        {
            return Table.ExecuteQuery(query);
        }
    
        public IEnumerable<T> GetEntities<T>(String partitionKey, Int32 noOfRecords ) where T : ITableEntity, new()
        {
            var query = new TableQuery<T>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey));
            var result = Table.ExecuteQuery(query).Take(noOfRecords).ToList();
            return result;
        }
    
        public T GetEntity<T>(String partitionKey, String rowKey) where T : class, ITableEntity, new()
        {
            var retrieveOperation = TableOperation.Retrieve<T>(partitionKey, rowKey);
    
            // Execute the retrieve operation.
            var retrievedResult = Table.Execute(retrieveOperation);
            return retrievedResult.Result as T;
        }
    
        public Boolean DeleleEntity<T>(String partitionKey, String rowKey) where T : class, ITableEntity, new()
        {
            TableOperation retrieveOperation = TableOperation.Retrieve<T>(partitionKey, rowKey);
    
            // Execute the operation.
            var retrievedResult = Table.Execute(retrieveOperation);
    
            // Assign the result to a CustomerEntity.
            var deleteEntity = (T)retrievedResult.Result;
    
            // Create the Delete TableOperation.
            if (deleteEntity != null)
            {
                TableOperation deleteOperation = TableOperation.Delete(deleteEntity);
    
                // Execute the operation.
                Table.Execute(deleteOperation);
            }
    
            return true;
        }
    
        public Boolean UpdateEntity<T>(String partitionKey, String rowKey) where T : class, ITableEntity, new()
        {
            TableOperation retrieveOperation = TableOperation.Retrieve<T>(partitionKey, rowKey);
    
            // Execute the operation.
            TableResult retrievedResult = Table.Execute(retrieveOperation);
    
            // Assign the result to a CustomerEntity object.
            var updateEntity = (T)retrievedResult.Result;
    
            if (updateEntity != null)
            {
                // Create the InsertOrReplace TableOperation
                TableOperation updateOperation = TableOperation.Replace(updateEntity);
    
                // Execute the operation.
                Table.Execute(updateOperation);
            }
    
            return true;
        }
    
        public Boolean UpdateEntity<T>(T entity) where T : class, ITableEntity, new()
        {
            Boolean isUpdate = false;
            try
            {
                // Create the InsertOrReplace TableOperation
                TableOperation updateOperation = TableOperation.Replace(entity);
    
                // Execute the operation.
                Table.Execute(updateOperation);
                isUpdate = true;
            }
            catch (Exception ex)
            {
                isUpdate = false;
            }
    
            return isUpdate;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 2012-08-12
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多