在一般情况下,当我们在.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. java.util.HashMap

    做LeeCode上的题目,发现关于数组的题目用HashMap后简化运算,包括在之前的工作中,也多次用到HashMap而我对它的了解却不多,现在来总结一下. 在算法中的用处,暂时的理解是,当数组中两个数 ...

  2. PHP执行系统命令

    <?phpexec("ping www.baidu.com -n 1",$output,$status);var_dump($output);var_dump($status ...

  3. 配置perl-cgi的运行环境,由于Active Perl安装在d:\perl

    Apache 1.3.22 for Win32+PHP 4.0.6+Active Perl 5.006001+Zend Optimizer v1.1.0+mod_gzip 1.3.19.1a+MySQ ...

  4. pch头文件

    1.command+N ---> Other ---> PCH File 2.点击工程 ---> Build Settings ---> 搜索框中输入pref ---> ...

  5. Jquery的命名冲突

    $是Jquery的别名,为了编码方便,我们可以使用$符号来调用Jquery的函数.然而,当我们引入多个JS库的时候,如果另外一个库中也引用了$符号作为别名的话,那么我们在使用$符号的时候,由于同一个作 ...

  6. 淘宝天猫网站停止支持IE6、IE7浏览器,你还在用xp吗?

    2016年4月14日,是科比正式告别篮球的最后一场球赛.大家都在忙着各种纪念和怀念着看科比打球的青葱岁月.不过已经完美谢幕.而我们今天要说的是微软的IE6.IE7浏览器.淘宝网和天猫商城正式停止支持I ...

  7. Thinking in Java——笔记(7)

    Reusing Classes The first is composition,You're simply reusing the functionality of the code, not it ...

  8. SeasLog-An effective,fast,stable log extension for PHP

    github: https://github.com/Neeke/SeasLog @author Chitao.Gao [neeke@php.net] @交流群 312910117 简介 为什么使用S ...

  9. UIBezierPath贝塞尔弧线常用方法记

    //根据一个矩形画曲线 + (UIBezierPath *)bezierPathWithRect:(CGRect)rect //根据矩形框的内切圆画曲线 + (UIBezierPath *)bezie ...

  10. PL/SQL不支持64位Oracle Client 解决办法

    解决X64操作系统PL/SQL连接报错问题 make sure you have the 32 bits oracle client installed 说明PLSQL Developer并不支持Or ...