zoukankan      html  css  js  c++  java
  • Windows Azure Table storage 之 动态Table类 DynamicTableEntity

    在一般情况下,当我们在.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();
           }
    
    
     
  • 相关阅读:
    spring boot图片上传至远程服务器并返回新的图片路径
    java错误笔记
    mybatis逆向工程
    将vue-cli项目配置在nginx上
    spring boot使用jpa查询mysql数据库的视图时不报错,但查询结果数据总是重复第一条
    match,location,history
    fetch和axios区别,proxy代理配置
    es6语法错误
    JAVA多线程之volatile 与 synchronized 的比较
    做到这一点,你也可以成为优秀的程序员
  • 原文地址:https://www.cnblogs.com/he-yuan/p/3357753.html
Copyright © 2011-2022 走看看