在一般情况下,当我们在.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. asp.net 网站开发流程总结

    由于这学期要做asp.net的网站开发,导师让我们在前期做详细的计划说明,时间安排.由于网站开发流程不知道,以及需要学什么指示都是盲懂,所以计划安排需在了解大致流程之后才能做出来,一下是询问同学和在网 ...

  2. Linux下配置Lamp

    linux下配置lamp步骤: 一.快速安装Apache+PHP5+MySql 先更新: # yum update 然后安装LAMP环境:(163的yum源上只有php5.1.6 mysql 5.0. ...

  3. 创建删除元素appendChild,removeChild,createElement,insertBefore

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  4. c#语句 随堂练习2

    1.方程ax²+bx+c=0是一元二次方程,求根. 2.输入一个年份 ,判断是不是闰年.(能被4整除但不能被100整除的年份是闰年,有的世纪年也是闰年) 3.标准体重:男士体重=身高-100±3:女士 ...

  5. 关于使用QQ、新浪微博、腾讯微博等第三方登录网站的开发过程(二)

    (二).新浪微博登录 1. 首先在新浪微博开放平台注册成为开发者.[http://open.weibo.com/connect] 具体自己填写一些相关信息就OK! 2. 注册成功之后,点击[微连接], ...

  6. mysql中字段类型转换排序

    表中字段server_id是varchar类型,现在我们查询数据时想以server_id排一下序,排序后的结果 select server_id from cardserver where game_ ...

  7. 运行在VMware上的Linux虚拟机如何使用NAT模式连接物理机的外部网络

    在VMware Workstation中,默认有3个虚拟交换机,分别是VMnet0(使用桥接网络).VMnet1(仅主机网络)和VMnet8(NAT网络). 首先说一下为什么要用NAT模式,如果你的物 ...

  8. html本地服务器

    html本地服务器 http://files.cnblogs.com/files/douxuyao/Aws.rar

  9. git总是出现untracked content怎么解决?

    git rm --cached vendor/plugins/open_flash_chart_2 rm -rf vendor/plugins/open_flash_chart_2/.git # BA ...

  10. 《最终幻想XV》中角色AI的意识决策系统解析

    http://gad.qq.com/article/detail/7155321