C# 带有.NET 4.5的用于表存储的Azure SDK

C# 带有.NET 4.5的用于表存储的Azure SDK,c#,azure,azure-storage,azure-table-storage,C#,Azure,Azure Storage,Azure Table Storage,我正在搜索一些关于如何使用Azure SDK在Azure表存储中存储和检索数据的示例或文档 我使用C#和.NET4.5框架 我在这里找到了文件 该部分: // Create the CloudTable object that represents the "people" table. CloudTable table = tableClient.GetTableReference("people"); // Create a new customer entity. CustomerEnt

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

我使用C#和.NET4.5框架

我在这里找到了文件

该部分:

// 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);
不再可用

有人知道如何使用.NET4.5实现这一点吗


致意

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

编辑:

要获取GetTableReference方法,需要执行以下操作:

  • 请参考Microsoft.WindowsAzure.Storage.dll版本2.0.0.0或更高版本(通过NuGet:
    安装程序包WindowsAzure.Storage
  • 添加以下名称空间:

  • 初始化存储帐户和表客户端


这是我的table storage manager类,我使用的是.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;
    }
}
公共类表存储管理器
{
私有云表客户端;
私有云表;
#区域Sigleton实现
公共表存储管理器(字符串表名)
{
TableClient=StorageFactory.GetCloudStorageAccount().CreateCloudTableClient();
Table=TableClient.GetTableReference(tablename);
//var ctx=TableClient.GetTableServiceContext();
Table.CreateIfNotExists();
}
#端区
public void InsertAnyEntity(T实体)
{
var translatedEntity=Helper.converttodictionary实体(实体);
插入性(翻译性);
}
公共无效插入实体(T实体),其中T:ITableEntity
{
Table.Execute(TableOperation.InsertOrReplace(entity));
}
public IEnumerable ExecuteQuery(TableQuery查询),其中T:class,ITableEntity,new()
{
返回表.ExecuteQuery(查询);
}
public IEnumerable GetEntities(String partitionKey,Int32 noOfRecords),其中T:ITableEntity,new()
{
var query=new TableQuery().Where(TableQuery.GenerateFilterCondition(“PartitionKey”,QueryComparisons.Equal,PartitionKey));
var result=Table.ExecuteQuery.Take(noOfRecords.ToList();
返回结果;
}
public T GetEntity(String partitionKey,String rowKey),其中T:class,ITableEntity,new()
{
var retrieveOperation=TableOperation.Retrieve(partitionKey,rowKey);
//执行检索操作。
var retrievedResult=Table.Execute(retrieveOperation);
返回retrievedResult,结果为T;
}
公共布尔DeleleEntity(String partitionKey,String rowKey),其中T:class,ITableEntity,new()
{
TableOperation retrieveOperation=TableOperation.Retrieve(partitionKey,rowKey);
//执行该操作。
var retrievedResult=Table.Execute(retrieveOperation);
//将结果分配给CustomerEntity。
var deleteEntity=(T)retrievedResult.Result;
//创建删除表操作。
if(deleteEntity!=null)
{
TableOperation deleteOperation=TableOperation.Delete(deleteEntity);
//执行该操作。
表。执行(删除操作);
}
返回true;
}
public Boolean UpdateEntity(String partitionKey,String rowKey),其中T:class,ITableEntity,new()
{
TableOperation retrieveOperation=TableOperation.Retrieve(partitionKey,rowKey);
//执行该操作。
TableResult retrievedResult=Table.Execute(retrieveOperation);
//将结果指定给CustomerEntity对象。
var updateEntity=(T)retrievedResult.Result;
if(updateEntity!=null)
{
//创建InsertOrReplace表操作
TableOperation updateOperation=TableOperation.Replace(updateEntity);
//执行该操作。
表。执行(更新操作);
}
返回true;
}
公共布尔更新属性(T实体),其中T:class,ITableEntity,new()
{
布尔值isUpdate=false;
尝试
{
//创建InsertOrReplace表操作
TableOperation updateOperation=TableOperation.Replace(实体);
//执行该操作。
表。执行(更新操作);
isUpdate=true;
}
捕获(例外情况除外)
{
isUpdate=false;
}
返回isUpdate;
}
}

tableClient或CloudTable类没有getTableReference()方法!您使用了哪个.NET版本?几天前我写了一篇博文,介绍了一些有关使用storage client library 2.0的简单示例,您可以在此处阅读:。嗯。
var account = new CloudStorageAccount(...);
var tableClient = account.CreateCloudTableClient();
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;
    }
}