First of all, create a Simple Console Application and call it "TableStorageSamples". Then add Azure.Data.Tables Nuget Package to your simple console app.
I do not think I should explain what I am going to do. Just look at the code I have published below.
string connectionString = "Your Connection String";
string tableName = "Orders";
The connection string can be found from your AZURE Portal, following the below picture.
By following the below code block, you can simply add an Entity into your AZURE Table Storage
#region Adding-Entity
void AddEntity(string orderID, string category, int quantity)
{
TableClient tableClient = new TableClient(connectionString, tableName);
TableEntity tableEntity = new TableEntity(category, orderID)
{
{"quantity",quantity}
};
tableClient.AddEntity(tableEntity);
//
Console.WriteLine("Added Entity with order ID {0}", orderID);
}
#endregion
If you are looking for a query against your data in AZURE Table Storage, find it here.
#region Reading-Entity
void QueryEntity(string category)
{
TableClient tableClient = new TableClient(connectionString, tableName);
Pageable<TableEntity> results = tableClient.Query<TableEntity>(entity => entity.PartitionKey == category);
foreach (TableEntity tableEntity in results)
{
Console.WriteLine("Order Id {0}", tableEntity.RowKey);
Console.WriteLine("Quantity is {0}", tableEntity.GetInt32("quantity"));
}
}
#endregion
You need to delete an Entity, look at the below section.
#region Delete-Entity
void DeleteEntity(string category, string orderID)
{
TableClient tableClient = new TableClient(connectionString, tableName);
tableClient.DeleteEntity(category, orderID);
Console.WriteLine("Entity with Partition Key {0} and Row Key {1} deleted", category, orderID);
}
#endregion
A simple update operation can be found here.
#region Update-Entity
void UpdateEntity(string orderID, string category, int quantity)
{
TableClient tableClient = new TableClient(connectionString, tableName);
TableEntity tableEntity = new TableEntity(category, orderID)
{
{"quantity",quantity}
};
tableClient.UpsertEntity(tableEntity);
//
Console.WriteLine("Entity with Partition Key {0} and Row Key {1} Updated", category, orderID);
}
#endregion
If you are lazy to try the blocks one by one, have a look at all the blocks plus how to use the methods below.
using Azure;
using Azure.Data.Tables;
string connectionString = "Your ConnectionString";
string tableName = "Orders";
//AddEntity("O1", "Mobile", 100);
//AddEntity("O2", "Laptop", 50);
//AddEntity("O3", "Desktop", 70);
//AddEntity("O4", "Laptop", 200);
//QueryEntity("Laptop");
//DeleteEntity("Laptop", "O2");
UpdateEntity("O4", "Laptop", 500);
#region Update-Entity
void UpdateEntity(string orderID, string category, int quantity)
{
TableClient tableClient = new TableClient(connectionString, tableName);
TableEntity tableEntity = new TableEntity(category, orderID)
{
{"quantity",quantity}
};
tableClient.UpsertEntity(tableEntity);
//
Console.WriteLine("Entity with Partition Key {0} and Row Key {1} Updated", category, orderID);
}
#endregion
#region Delete-Entity
void DeleteEntity(string category, string orderID)
{
TableClient tableClient = new TableClient(connectionString, tableName);
tableClient.DeleteEntity(category, orderID);
Console.WriteLine("Entity with Partition Key {0} and Row Key {1} deleted", category, orderID);
}
#endregion
#region Reading-Entity
void QueryEntity(string category)
{
TableClient tableClient = new TableClient(connectionString, tableName);
Pageable<TableEntity> results = tableClient.Query<TableEntity>(entity => entity.PartitionKey == category);
foreach (TableEntity tableEntity in results)
{
Console.WriteLine("Order Id {0}", tableEntity.RowKey);
Console.WriteLine("Quantity is {0}", tableEntity.GetInt32("quantity"));
}
}
#endregion
#region Adding-Entity
void AddEntity(string orderID, string category, int quantity)
{
TableClient tableClient = new TableClient(connectionString, tableName);
TableEntity tableEntity = new TableEntity(category, orderID)
{
{"quantity",quantity}
};
tableClient.AddEntity(tableEntity);
//
Console.WriteLine("Added Entity with order ID {0}", orderID);
}
#endregion
Wish you all the best!
Ali
Top comments (0)