简体   繁体   English

适用于.NET 4.5的TableStorage Azure SDK

[英]Azure SDK for TableStorage with .NET 4.5

I am searching for some examples or documentation on how to use the Azure SDK to store and retrieve data in Azure Table Storage. 我正在搜索有关如何使用Azure SDK在Azure表存储中存储和检索数据的一些示例或文档。

I use C# with .NET 4.5 framework. 我使用C#和.NET 4.5框架。

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

the part: 那个部分:

// 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);

is not available anymore. 目前不再可用。

Does anybody know how to do this with .NET 4.5? 有谁知道如何使用.NET 4.5做到这一点?

Best Regards 最好的祝福

This code works fine. 这段代码工作正常。 While the documentation mentions version 2.0, this points to the version of the storage SDK and not the version of .NET. 虽然文档提到了2.0版,但这指的是存储SDK的版本,而不是.NET的版本。

Edit: 编辑:

In order to get the GetTableReference method you need to do the following: 要获取GetTableReference方法,您需要执行以下操作:

  • Reference Microsoft.WindowsAzure.Storage.dll version 2.0.0.0 or higher (via NuGet: Install-Package WindowsAzure.Storage ) 参考Microsoft.WindowsAzure.Storage.dll版本2.0.0.0或更高版本(通过NuGet: Install-Package WindowsAzure.Storage
  • Add the following namespaces: 添加以下命名空间:

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Table;
  • Initialize the storage account and the table client. 初始化存储帐户和表客户端。

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

This is my table storage manager class, i am using .NET 4.5 这是我的表存储管理器类,我使用的是.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;
    }
}

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

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