在一般情况下,当我们在.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. inconfont 字体库应用

    先去注册个号码,好像只可以用新浪微博登录哈,搞一个微博去. 第一就是点上面图标库,选择官方和所有都行. 恩接着点一个图标,他就自己跑到 第二个按钮哪里去了,在点第二个按钮,会出来一个创建项目,随便创建 ...

  2. Maching Learning 学习资料

    A星(A*, A Star)算法详解 CSDN技术主题月----“深度学习”代码笔记专栏 UC Berkeley CS188 Intro to AI

  3. C# 安装和卸载 Windows Service

    特别注意: 安装Window Service 的时候,一定要用管理员打开命令提示符(cmd) 1. 创建Windows Service 服务项目 2. Service设计界面:右键-->选择安装 ...

  4. C# 在 vs2010 上开发 ActiveX 控件 【千辛万苦啊~】

    由于B/S项目中需要使用到读卡器的功能,但是由于厂家只有提供一个 读卡的dll,那么怎样能在客户端使用读卡器呢,那么进过一番查找,最总决定使用ActiveX 控件来做.由于是第一次接触到 Active ...

  5. Oracle加密表空间进行数据加密的示例

    接上篇:http://www.cnblogs.com/myrunning/p/4292049.html 1查看数据库版本 2查看当前数据库表空间 从这里看到我们此时数据库里没有加密表空间. 3创建加密 ...

  6. 未能加载文件或程序集“Newtonsoft.Json, Version=4.0.0.0, Culture=neutral, PublicKeyToken=30a [问题点数:40分,结帖人u010259408]

    未能加载文件或程序集“Newtonsoft.Json, Version=4.0.0.0, Culture=neutral, PublicKeyToken=30a [问题点数:40分,结帖人u01025 ...

  7. 【iCore3 双核心板】例程十八:USB_VCP实验——虚拟串口

    实验指导书及代码包下载: http://pan.baidu.com/s/1c1erqIc iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  8. Asp.net DropDownList 自定义样式(想怎么改就怎么改!)

    最近在做一个asp.net的项目,需要对默认的dropdownlist样式进行美化,固有的dropdownlist的小箭头实在让人无法接受,于是开始在百度,google 上下求索,天不负有心人,终于找 ...

  9. ExtJS笔记 Grids

    参考:http://blog.csdn.net/zhangxin09/article/details/6885175 The Grid Panel is one of the centerpieces ...

  10. C#记录程序耗时的方法

    用于准确测量运行时间的方法类: System.Diagnostics.Stopwatch 具体使用方法: using System.Diagnostics; Stopwatch stopwatch = ...