DEV Community

Ali Kolahdoozan
Ali Kolahdoozan

Posted on

Insert-Update-Delete and Read operations in AZURE Table Storage by C#.NET !

First of all, create a Simple Console Application and call it "TableStorageSamples". Then add Azure.Data.Tables Nuget Package to your simple console app.

Image description

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.

Image description

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


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

Wish you all the best!
Ali

Top comments (0)