TableMananger

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;

namespace AzureTableStorageCRUDoperation
{
public class TableManager
{
///
/// Cloud Table object
///
private CloudTable table;

    /// <summary>
    /// Create Cloud Table object
    /// </summary>
    /// <param name="TableName">Name of the Table Name</param>
    public TableManager(string TableName)
    {
        // Check if Table Name is blank
        if (string.IsNullOrEmpty(TableName))
        {
            throw new ArgumentNullException("Table", "Table Name can't be empty");
        }
        try
        {
            // Get azure table storage connection string.
            string ConnectionString = "UseDevelopmentStorage=true";
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);

            // Create the table if not exist and put the refarence of the table into Cloud Table object
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
            table = tableClient.GetTableReference(TableName);
            table.CreateIfNotExists();
        }
        catch (Exception ExceptionObj)
        {
            throw ExceptionObj;
        }
    }

    /// <summary>
    /// Insert or Update Method
    /// </summary>
    /// <typeparam name="T">Entity Type</typeparam>
    /// <param name="entity">T type object</param>
    /// <param name="forInsert">true for Insert, false for Update</param>
    public void InsertEntity<T>(T entity, bool forInsert = true) where T : TableEntity, new()
    {
        try
        {
            if (forInsert)
            {
                var insertOperation = TableOperation.Insert(entity);
                table.Execute(insertOperation);
            }
            else
            {
                var insertOrMergeOperation = TableOperation.InsertOrReplace(entity);
                table.Execute(insertOrMergeOperation);
            }
        }
        catch (Exception ExceptionObj)
        {
            throw ExceptionObj;
        }
    }

    /// <summary>
    /// Retrieve List of T type entity
    /// </summary>
    /// <typeparam name="T">Returned Entity Type</typeparam>
    /// <param name="Query"></param>
    /// <returns>List of T type object</returns>
    public List<T> RetrieveEntity<T>(string Query = null) where T : TableEntity, new()
    {
        try
        {
            TableQuery<T> DataTableQuery = new TableQuery<T>();
            if (!string.IsNullOrEmpty(Query))
            {
                DataTableQuery = new TableQuery<T>().Where(Query);
            }
            IEnumerable<T> IDataList = table.ExecuteQuery(DataTableQuery);
            List<T> DataList = new List<T>();
            foreach (var singleData in IDataList)
                DataList.Add(singleData);
            return DataList;
        }
        catch (Exception ExceptionObj)
        {
            throw ExceptionObj;
        }
    }

    /// <summary>
    /// Delete the entity
    /// </summary>
    /// <typeparam name="T">Entity Type</typeparam>
    /// <param name="entity">T type entity</param>
    /// <returns>true if able to delete</returns>
    public bool DeleteEntity(TableEntity entity) 
    {
        try
        {
            var DeleteOperation = TableOperation.Delete(entity);
            table.Execute(DeleteOperation);
            return true;
        }
        catch (Exception ExceptionObj)
        {
            throw ExceptionObj;
        }
    }
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值