在一般情况下,当我们在.net中使用Azure table storage的时候都会为该表建立一个TableEntity的派生类,如下所示。

public class CustomerEntity : TableEntity

{

    public CustomerEntity(string lastName, string firstName)

    {

        this.PartitionKey = lastName;

        this.RowKey = firstName;

    }

    public CustomerEntity() { }

    public string Email { get; set; }

    public string PhoneNumber { get; set; }

}

  

这是建立在我们知道表结构的基础上的。

但在某些时候我们无法知道Table storage里的表的结构的时候这种办法就无法奏效了。

例如我们需要写一个软件帮助用户管理他们的table storage, 这时我们无法知道用户的表结构所以不能够使用上面这种结构。

这时使用Azure storage提供的另外一个类能够很好的解决我们的问题那就是DynamicTableEntity类,该类包含在Microsoft.WindowsAzure.Storage.Table之中。如TableEntity一样提供了Table 必须包含的几种属性,同时提供了一个

IDictionary<string, EntityProperty> Properties 属性。

该属性能够将除开Azure table storage必须包含的几个属性之外的其他属性以key value的形式包含在这个Properties之中。

这样我们就能够无需声明特定的Entity类也能够用这个类来处理相关的操作。

以下代码是通过使用这个类来获取一个表中的所有信息,从而将它展示在Gridview中。

protected void btnQuery_Click(object sender, EventArgs e)
{
CloudStorageAccount account = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudTableClient client = account.CreateCloudTableClient();
CloudTable table = client.GetTableReference("DynamicEntityTable"); var query = new TableQuery();
var tableResults = table.ExecuteQuery(query); DataTable propertiesTable = new DataTable("DynamicEntity"); //A Dynamic Entity Table must have the properties in ITableEntity.
DataColumn partitionKeyColumn = new DataColumn();
partitionKeyColumn.DataType = Type.GetType("System.String");
partitionKeyColumn.ColumnName = "Partition Key";
propertiesTable.Columns.Add(partitionKeyColumn); DataColumn rowKeyColumn = new DataColumn();
rowKeyColumn.DataType = Type.GetType("System.String");
rowKeyColumn.ColumnName = "Row Key";
propertiesTable.Columns.Add(rowKeyColumn); //Dynamic Entity Table have a property called Proerties which include other table column as KeyValue pair.
foreach (var entity in tableResults)
{
DataRow row;
row = propertiesTable.NewRow();
row["Partition Key"] = entity.PartitionKey;
row["Row Key"] = entity.RowKey;
if (entity.Properties != null)
{
foreach (var kvp in entity.Properties)
{
if (!propertiesTable.Columns.Contains(kvp.Key))
{
DataColumn column = new DataColumn();
column.ColumnName = kvp.Key;
column.DataType = Type.GetType("System." + kvp.Value.PropertyType.ToString());
propertiesTable.Columns.Add(column);
} switch (kvp.Value.PropertyType)
{
case EdmType.Binary:
row[kvp.Key] = kvp.Value.BinaryValue;
break;
case EdmType.Boolean:
row[kvp.Key] = kvp.Value.BooleanValue;
break;
case EdmType.DateTime:
row[kvp.Key] = kvp.Value.DateTimeOffsetValue;
break;
case EdmType.Double:
row[kvp.Key] = kvp.Value.DoubleValue;
break;
case EdmType.Guid:
row[kvp.Key] = kvp.Value.GuidValue;
break;
case EdmType.Int32:
row[kvp.Key] = kvp.Value.Int32Value;
break;
case EdmType.Int64:
row[kvp.Key] = kvp.Value.Int64Value;
break;
case EdmType.String:
row[kvp.Key] = kvp.Value.StringValue;
break;
default:
break;
} }
}
propertiesTable.Rows.Add(row);
}
gvwAzureTable.DataSource = propertiesTable;
gvwAzureTable.DataBind();
}

 

Windows Azure Table storage 之 动态Table类 DynamicTableEntity的更多相关文章

  1. [Windows Azure] How to use the Windows Azure Blob Storage Service in .NET

    How to use the Windows Azure Blob Storage Service in .NET version 1.7 version 2.0 This guide will de ...

  2. [Windows Azure] How to use the Table Storage Service

    How to use the Table Storage Service version 1.7 version 2.0 This guide will show you how to perform ...

  3. 在 Windows Azure 网站中配置动态 IP 地址限制

    我们最近对 Windows Azure 网站进行了升级,并启用了IIS8的动态 IP 限制模块.现在,开发人员可以为其网站启用并配置动态 IP 限制功能(或简称 DIPR). 可以通过以下链接查看此 ...

  4. Azure Table storage 之改进DynamicTableEntity类为其添加动态语言扩展

    在之前的一篇文章中提到,storage类库中包含一个可以用来动态获取Azure table storage 表结构的类-DynamicTableEntity. 我们可以通过这个类,我们无需为每一个表提 ...

  5. Windows Azure Storage (6) Windows Azure Storage之Table

    <Windows Azure Platform 系列文章目录> 最近想了想,还是有必要把Windows Azure Table Storage 给说清楚. 1.概念 Windows Azu ...

  6. Windows Azure Storage

    之前都是在博客园看别人的文章,今天开始就开启自己的博客咯,欢迎阅读,共同探讨! 简单点说Widows Azure Storage就是一个大的网盘,可以让用户存储任何想存储的数据,数据一旦存储到“云”中 ...

  7. [Windows Azure] How to use the Queue Storage Service

    How to use the Queue Storage Service version 1.7 version 2.0 This guide will show you how to perform ...

  8. [Windows Azure] .NET Multi-Tier Application Using Storage Tables, Queues, and Blobs - 1 of 5

    .NET Multi-Tier Application Using Storage Tables, Queues, and Blobs - 1 of 5 This tutorial series sh ...

  9. [Windows Azure] Enabling Diagnostics in Windows Azure

    Enabling Diagnostics in Windows Azure Windows Azure Diagnostics enables you to collect diagnostic da ...

随机推荐

  1. SpringMVC+Shiro权限管理

    什么是权限呢?举个简单的例子: 我有一个论坛,注册的用户分为normal用户,manager用户.对论坛的帖子的操作有这些:添加,删除,更新,查看,回复我们规定:normal用户只能:添加,查看,回复 ...

  2. Func<T, TResult> Delegate

    public delegate TResult Func<in T, out TResult>( T arg ) http://msdn.microsoft.com/en-us/libra ...

  3. CSS3两个动画顺序衔接播放

    问题描述: 第一个动画先播放,播放完成后,第二个动画紧接着播放. 解决办法: 1. 将第二个的延迟时间(animation-delay) 设置成第一个的持续时间( animation-duration ...

  4. MyBatis增删改查

    MyBatis的简介: MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名 ...

  5. Linux_用户/用户组

    一.用户添加 1.  账号添加 [root@hadoop09-linux tmp]# useradd eRrsr 这时/etc/passwd文件中会追加该用户项,并且在/home文件夹下自动生成该属于 ...

  6. childNodes 和children

    childNodes 兼容性不是很好,一般用children 元素.childNodes : 只读 属性 子节点列表集合标准下:包含了空白换行和元素类型的节点,也会包含非法嵌套的子节点非标准下:只包含 ...

  7. sign in和sign up区别

    如果是网站的话sign up是注册,sign in是登录的意思,另外,sign out退出

  8. 理解group by 语句的扩展使用

    在SQL的开发中我们会经常使用group by语句对数据进行分组统计,然而在一些复杂的BI报表开发中会常遇到更复杂的分组需求,单单使用group by 就不能解决我们的问题了,这时我们就需要学习了解一 ...

  9. bzoj1855: [Scoi2010]股票交易--单调队列优化DP

    单调队列优化DP的模板题 不难列出DP方程: 对于买入的情况 由于dp[i][j]=max{dp[i-w-1][k]+k*Ap[i]-j*Ap[i]} AP[i]*j是固定的,在队列中维护dp[i-w ...

  10. linux同步系统时间

    命令:ntpdate 路径:/usr/sbin/ntpdate 例子:ntpdate us.pool.ntp.org 查看日期时间命令:date 修改日期时间命令:date -s "2012 ...