在一般情况下,当我们在.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. mysqlbinglog基于即时点还原

    mysqlbinlog介绍 要想从二进制日志恢复数据,你需要知道当前二进制日志文件的路径和文件名.一般可以从选项文件(即my.cnf or my.ini,取决于你的系统)中找到路径. (mysql5. ...

  2. WIN8 MTK驱动不能安装解决办法

    1.把鼠标移动到桌面最右下角的位置会出来一个侧边栏,按那个齿轮就是“设置”,会出来个菜单,选择最下边的“更多电脑设置” 注:也可以按快捷键“WIN+I” 2.选择“常规”→“高级启动”→”立即重启“ ...

  3. 汇编寄存器(内存访问)基础知识之三---mov指令

     1 内存中字的存储 一个字型数据占2个内存单元,内存里面一个内存单元一个字节(8位),高地址单位放高8位,低地址单元放低8位. 注意:0号是地址单元,1是高地址单元(上是低地址,下面是高地址) (1 ...

  4. CSS3初学篇章_4(边框样式/段落样式)

    边框样式 1.边框线语法:border-style : none | hidden | dotted | dashed | solid | double | groove | ridge | inse ...

  5. 安装repcached,并且测试其双向复制是否成功

    备注:本实验不仅包括了repcached,还包括了memcache的配置安装 1.1实验环境. 1.2环境准备. 1.3配置一个memcache. 1.3.1安装memcache. 1.3.2启动me ...

  6. html - 自动播放音乐

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. ThinkPHP 3.2.3 简单后台模块开发(一)常用配置

    一.项目分组 下载解压 ThinkPHP 3.2.3,在默认的应用 Application(./Application) 中,包含一个默认的模块 Home(./Application/Home). 需 ...

  8. 在MVC3中修改KindEditor实现图片删除

    编辑器KindEditor可以上传图片,但却不能删除图片,因此我们通过修改一些文件,对KindEditor进行扩展,使得KindEditor能删除服务器上的图片. 主要方法就是:在图片空间中浏览图片, ...

  9. spring与redis简单整合

    项目结构 整合需要的依赖 <dependencies> <dependency> <groupId>org.springframework</groupId& ...

  10. LeetCode Zigzag Iterator

    原题链接在这里:https://leetcode.com/problems/zigzag-iterator/ 题目: Given two 1d vectors, implement an iterat ...