public void CreateDeleteTable() { string storageUri = StorageUri; string accountName = StorageAccountName; string storageAccountKey = PrimaryStorageAccountKey; string tableName = "OfficeSupplies1p1"; #region Snippet:TablesSample1CreateClient // Construct a new <see cref="TableServiceClient" /> using a <see cref="TableSharedKeyCredential" />. var serviceClient = new TableServiceClient( new Uri(storageUri), new TableSharedKeyCredential(accountName, storageAccountKey)); #endregion try { #region Snippet:TablesSample1CreateTable // Create a new table. The <see cref="TableItem" /> class stores properties of the created table. TableItem table = serviceClient.CreateTable(tableName); Console.WriteLine($"The created table's name is {table.TableName}."); #endregion } finally { #region Snippet:TablesSample1DeleteTable // Deletes the table made previously. serviceClient.DeleteTable(tableName); #endregion } }
public void TableCreateError() { string storageUri = StorageUri; string accountName = AccountName; string storageAccountKey = PrimaryStorageAccountKey; string tableName = "OfficeSupplies2"; var serviceClient = new TableServiceClient( new Uri(storageUri), new TableSharedKeyCredential(accountName, storageAccountKey)); #region Snippet:TablesSample1CreateExistingTable try { // Creates a table. serviceClient.CreateTable(tableName); // Second attempt to create table with the same name should throw exception. serviceClient.CreateTable(tableName); } catch (RequestFailedException e) { Console.WriteLine("Create existing table throws the following exception:"); Console.WriteLine(e.Message); } #endregion finally { serviceClient.DeleteTable(tableName); } }
public void QueryTables() { string storageUri = StorageUri; string accountName = StorageAccountName; string storageAccountKey = PrimaryStorageAccountKey; string tableName = "OfficeSupplies3p1"; var serviceClient = new TableServiceClient( new Uri(storageUri), new TableSharedKeyCredential(accountName, storageAccountKey)); serviceClient.CreateTable(tableName); #region Snippet:TablesSample3QueryTables // Use the <see cref="TableServiceClient"> to query the service. Passing in OData filter strings is optional. Pageable <TableItem> queryTableResults = serviceClient.GetTables(filter: $"TableName eq '{tableName}'"); Console.WriteLine("The following are the names of the tables in the query results:"); // Iterate the <see cref="Pageable"> in order to access queried tables. foreach (TableItem table in queryTableResults) { Console.WriteLine(table.Name); } #endregion serviceClient.DeleteTable(tableName); }
public void UpdateUpsertEntities() { string storageUri = StorageUri; string accountName = StorageAccountName; string storageAccountKey = PrimaryStorageAccountKey; string tableName = "OfficeSupplies5p1"; string partitionKey = "Stationery"; string rowKey = "A1"; var serviceClient = new TableServiceClient( new Uri(storageUri), new TableSharedKeyCredential(accountName, storageAccountKey)); serviceClient.CreateTable(tableName); var tableClient = serviceClient.GetTableClient(tableName); #region Snippet:TablesSample5UpsertEntity var entity = new TableEntity(partitionKey, rowKey) { { "Product", "Markers" }, { "Price", 5.00 }, { "Brand", "myCompany" } }; // Entity doesn't exist in table, so invoking UpsertEntity will simply insert the entity. tableClient.UpsertEntity(entity); #endregion #region Snippet:TablesSample5UpsertWithReplace // Delete an entity property. entity.Remove("Brand"); // Entity does exist in the table, so invoking UpsertEntity will update using the given UpdateMode, which defaults to Merge if not given. // Since UpdateMode.Replace was passed, the existing entity will be replaced and delete the "Brand" property. tableClient.UpsertEntity(entity, TableUpdateMode.Replace); #endregion #region Snippet:TablesSample5UpdateEntity // Get the entity to update. TableEntity qEntity = tableClient.GetEntity <TableEntity>(partitionKey, rowKey); qEntity["Price"] = 7.00; // Since no UpdateMode was passed, the request will default to Merge. tableClient.UpdateEntity(qEntity, qEntity.ETag); TableEntity updatedEntity = tableClient.GetEntity <TableEntity>(partitionKey, rowKey); Console.WriteLine($"'Price' before updating: ${entity.GetDouble("Price")}"); Console.WriteLine($"'Price' after updating: ${updatedEntity.GetDouble("Price")}"); #endregion serviceClient.DeleteTable(tableName); }
public void QueryEntities() { string storageUri = StorageUri; string accountName = StorageAccountName; string storageAccountKey = PrimaryStorageAccountKey; string tableName = "OfficeSupplies4p1"; string partitionKey = "somePartition"; string rowKey = "1"; string rowKey2 = "2"; var serviceClient = new TableServiceClient( new Uri(storageUri), new TableSharedKeyCredential(accountName, storageAccountKey)); serviceClient.CreateTable(tableName); var tableClient = serviceClient.GetTableClient(tableName); var entity = new TableEntity(partitionKey, rowKey) { { "Product", "Markers" }, { "Price", 5.00 }, { "Quantity", 34 } }; tableClient.AddEntity(entity); var entity2 = new TableEntity(partitionKey, rowKey2) { { "Product", "Planner" }, { "Price", 7.00 }, { "Quantity", 34 } }; tableClient.AddEntity(entity2); #region Snippet:TablesSample4QueryEntitiesFilter Pageable <TableEntity> queryResultsFilter = tableClient.Query <TableEntity>(filter: $"PartitionKey eq '{partitionKey}'"); // Iterate the <see cref="Pageable"> to access all queried entities. foreach (TableEntity qEntity in queryResultsFilter) { Console.WriteLine($"{qEntity.GetString("Product")}: {qEntity.GetDouble("Price")}"); } Console.WriteLine($"The query returned {queryResultsFilter.Count()} entities."); #endregion #region Snippet:TablesSample4QueryEntitiesFilterWithQueryFilter // The CreateQueryFilter method is also available to assist with properly formatting and escaping OData queries. #if SNIPPET Pageable <TableEntity> queryResultsFilter = tableClient.Query <TableEntity>(filter: TableClient.CreateQueryFilter($"PartitionKey eq {partitionKey}")); #else queryResultsFilter = tableClient.Query <TableEntity>(filter: TableClient.CreateQueryFilter($"PartitionKey eq {partitionKey}")); #endif // Iterate the <see cref="Pageable"> to access all queried entities. foreach (TableEntity qEntity in queryResultsFilter) { Console.WriteLine($"{qEntity.GetString("Product")}: {qEntity.GetDouble("Price")}"); } Console.WriteLine($"The query returned {queryResultsFilter.Count()} entities."); // It handles esca #endregion #region Snippet:TablesSample4QueryEntitiesExpression // Use the <see cref="TableClient"> to query the table using a filter expression. double priceCutOff = 6.00; Pageable <OfficeSupplyEntity> queryResultsLINQ = tableClient.Query <OfficeSupplyEntity>(ent => ent.Price >= priceCutOff); #endregion #region Snippet:TablesSample4QueryEntitiesSelect Pageable <TableEntity> queryResultsSelect = tableClient.Query <TableEntity>(select: new List <string>() { "Product", "Price" }); #endregion #region Snippet:TablesSample4QueryEntitiesMaxPerPage Pageable <TableEntity> queryResultsMaxPerPage = tableClient.Query <TableEntity>(maxPerPage: 10); // Iterate the <see cref="Pageable"> by page. foreach (Page <TableEntity> page in queryResultsMaxPerPage.AsPages()) { Console.WriteLine("This is a new page!"); foreach (TableEntity qEntity in page.Values) { Console.WriteLine($"# of {qEntity.GetString("Product")} inventoried: {qEntity.GetInt32("Quantity")}"); } } #endregion serviceClient.DeleteTable(tableName); }
public void UpdateUpsertEntities() { string storageUri = StorageUri; string accountName = StorageAccountName; string storageAccountKey = PrimaryStorageAccountKey; string tableName = "OfficeSupplies5p1"; string partitionKey = "somePartition"; string rowKey = "A1"; var serviceClient = new TableServiceClient( new Uri(storageUri), new TableSharedKeyCredential(accountName, storageAccountKey)); serviceClient.CreateTable(tableName); try { #region Snippet:TablesSample5UpsertEntity // Get a reference to the <see cref="TableClient" /> of the table. var client = serviceClient.GetTableClient(tableName); // Make an entity. var entity = new Dictionary <string, object> { { "PartitionKey", partitionKey }, { "RowKey", rowKey }, { "Product", "Markers" }, { "Price", 5.00 }, { "Brand", "myCompany" } }; // Entity doesn't exist in table, so invoking UpsertEntity will simply insert the entity. client.UpsertEntity(entity); // Delete an entity property. entity.Remove("Brand"); // Entity does exist in the table, so invoking UpsertEntity will update using the given UpdateMode (which defaults to Merge if not given). // Since UpdateMode.Replace was passed, the existing entity will be replaced and delete the "Brand" property. client.UpsertEntity(entity, TableUpdateMode.Replace); #endregion #region Snippet:TablesSample5UpdateEntity // Query for entities to update. Pageable <IDictionary <string, object> > queryResultsBefore = client.Query(); foreach (IDictionary <string, object> qEntity in queryResultsBefore) { // Changing property of entity. qEntity["Price"] = 7.00; // Extract ETag from the entity. string eTag = qEntity["odata.etag"] as string; // Updating to changed entity using its generated eTag. // Since no UpdateMode was passed, the request will default to Merge. client.UpdateEntity(qEntity, eTag); } #endregion Pageable <IDictionary <string, object> > queryResultsAfter = client.Query(); foreach (IDictionary <string, object> qEntity in queryResultsAfter) { Console.WriteLine($"'Price' before updating: ${entity["Price"]}"); Console.WriteLine($"'Price' after updating: ${qEntity["Price"]}"); } } finally { serviceClient.DeleteTable(tableName); } }
public void CreateDeleteEntity() { string storageUri = StorageUri; string accountName = StorageAccountName; string storageAccountKey = PrimaryStorageAccountKey; string tableName = "OfficeSupplies2p1"; string partitionKey = "somePartition"; string rowKey = "A1"; string rowKeyStrong = "B1"; var serviceClient = new TableServiceClient( new Uri(storageUri), new TableSharedKeyCredential(accountName, storageAccountKey)); try { #region Snippet:TablesSample2GetTableClient // Construct a new <see cref="TableClient" /> using a <see cref="TableSharedKeyCredential" />. var client = new TableClient( tableName, new Uri(storageUri), new TableSharedKeyCredential(accountName, storageAccountKey)); #endregion #region Snippet:TablesSample2CreateEntity // Make an entity by defining a <see cref="Dictionary"> that includes the partition and row key. var entity = new Dictionary <string, object> { { "PartitionKey", partitionKey }, { "RowKey", rowKey }, { "Product", "Markers" }, { "Price", 5.00 }, }; // Insert the newly created entity. client.CreateEntity(entity); #endregion #region Snippet:TablesSample2CreateStronglyTypedEntity // Make a strongly typed entity by defining a custom class that extends <see cref="TableEntity">. var strongEntity = new OfficeSupplyEntity { PartitionKey = partitionKey, RowKey = rowKeyStrong, Product = "Notebook", Price = 3.00 }; // Insert the newly created entity. client.CreateEntity(strongEntity); #endregion #region Snippet:TablesSample2DeleteEntity // Delete the entity given the partition and row key. client.DeleteEntity(partitionKey, rowKey); #endregion } finally { serviceClient.DeleteTable(tableName); } }
public void CreateDeleteTable() { string storageUri = StorageUri; string accountName = StorageAccountName; string storageAccountKey = PrimaryStorageAccountKey; string tableName = "OfficeSupplies1p1"; #region Snippet:TablesSample1CreateClient // Construct a new "TableServiceClient using a TableSharedKeyCredential. var serviceClient = new TableServiceClient( new Uri(storageUri), new TableSharedKeyCredential(accountName, storageAccountKey)); #endregion #region Snippet:TablesSample1CreateTable // Create a new table. The TableItem class stores properties of the created table. #if SNIPPET string tableName = "OfficeSupplies1p1"; #endif TableItem table = serviceClient.CreateTableIfNotExists(tableName); Console.WriteLine($"The created table's name is {table.Name}."); #endregion #region Snippet:TablesMigrationCreateTableWithClient // Get a reference to the TableClient from the service client instance. var tableClient = serviceClient.GetTableClient(tableName); // Create the table if it doesn't exist. tableClient.CreateIfNotExists(); #endregion #region Snippet:TablesSample1DeleteTable // Deletes the table made previously. #if SNIPPET string tableName = "OfficeSupplies1p1"; #endif serviceClient.DeleteTable(tableName); #endregion #region Snippet:TablesSample1GetTableClient #if SNIPPET string tableName = "OfficeSupplies1p2"; var tableClient = serviceClient.GetTableClient(tableName); #else tableName = "OfficeSupplies1p2"; tableClient = serviceClient.GetTableClient(tableName); #endif #endregion #region Snippet:TablesSample1CreateTableClient #if SNIPPET var tableClient = new TableClient( #else tableClient = new TableClient( #endif new Uri(storageUri), tableName, new TableSharedKeyCredential(accountName, storageAccountKey)); #endregion #region Snippet:TablesSample1TableClientCreateTable tableClient.CreateIfNotExists(); #endregion #region Snippet:TablesSample1TableClientDeleteTable tableClient.Delete(); #endregion }
public void QueryEntities() { string storageUri = StorageUri; string accountName = StorageAccountName; string storageAccountKey = PrimaryStorageAccountKey; string tableName = "OfficeSupplies4p1"; string partitionKey = "somePartition"; string rowKey = "1"; string rowKey2 = "2"; var serviceClient = new TableServiceClient( new Uri(storageUri), new TableSharedKeyCredential(accountName, storageAccountKey)); serviceClient.CreateTable(tableName); try { var client = serviceClient.GetTableClient(tableName); var entity = new Dictionary <string, object> { { "PartitionKey", partitionKey }, { "RowKey", rowKey }, { "Product", "Markers" }, { "Price", 5.00 }, }; client.CreateEntity(entity); var entity2 = new Dictionary <string, object> { { "PartitionKey", "another" }, { "RowKey", rowKey2 }, { "Product", "Chair" }, { "Price", 7.00 }, }; client.CreateEntity(entity2); #region Snippet:TablesSample4QueryEntities // Use the <see cref="TableClient"> to query the table. Passing in OData filter strings is optional. Pageable <IDictionary <string, object> > queryResults = client.Query(filter: $"PartitionKey eq '{partitionKey}'"); // Iterate the <see cref="Pageable"> in order to access individual queried entities. foreach (IDictionary <string, object> qEntity in queryResults) { Console.WriteLine(qEntity["Product"]); } Console.WriteLine($"The query returned {queryResults.Count()} entities."); #endregion #region Snippet:TablesSample4QueryEntitiesExpressionTree // Use the <see cref="TableClient"> to query the table using a filter expression. double priceCutOff = 6.00; Pageable <OfficeSupplyEntity> queryResultsLINQ = client.Query <OfficeSupplyEntity>(ent => ent.Price >= priceCutOff); // Iterate the <see cref="Pageable"> in order to access individual queried entities. foreach (OfficeSupplyEntity qEntity in queryResultsLINQ) { Console.WriteLine($"{qEntity.Product}: ${qEntity.Price}"); } Console.WriteLine($"The LINQ query returned {queryResultsLINQ.Count()} entities."); #endregion } finally { serviceClient.DeleteTable(tableName); } }