首页 > 数据库 > > Windows Azure Table storage
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(); }
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70000523/viewspace-3156203/ ,如需转载,请注明出处,否则将追究法律责任。