Basic Operations on Windows Azure Table in Windows Azure Storage in Storage Client Library 2.0

In this article we will have a look at performing basic operations on a Windows Azure table using Windows Azure Storage Client Library 2.0.

To work with Windows Azure Storage Client 2.0, grab the library from the NuGet package. Right-click on the project and select "Manage NuGet Packages". In the NuGet Package Manager type WindowsAzure.Storage to search. Click on Install to add the library to the project.

Windows-Azure-Storage-Client 2.0-grab.jpg

After adding references of the Windows Azure Storage Client Library version 2.0, you need to add the following namespace:

Figure1.jpg

We can create a table like the following. Create an instance of CloudStorageAccount. To create this instance you need to pass a StorageCredentials object. The second parameter in creating the CloudStorageAccount is a boolean value of true. It says to use a https connection while working with the Windows Azure Storage account.

Figure2.jpg

After creating an object of the storage account, we need to create an instance of the CloudTabaleClient. We can create that as in the following:

Figure3.jpg

The last step in creating the Azure Table is to make a reference of the CloudTable and create it if it does not exist. Studenttable is the name of the table that we intend to create here.

Figure4.jpg

A Table can be deleted as in the following:

Figure5.jpg

Consolidating all the preceding discussion, we can create a table with a HTTPS connection using Windows Azure Storage Client Library 2.0 is in the following:

using Microsoft.WindowsAzure.Storage;

using Microsoft.WindowsAzure.Storage.Auth;

using Microsoft.WindowsAzure.Storage.Table;

using System;

 

namespace AzureTableStorage

{

    class Program

    {

        static void Main(string[] args)

        {

            CloudStorageAccount account = new CloudStorageAccount(new StorageCredentials("youraccountname", "youraccountkey"),true);

            CloudTableClient tableClient = account.CreateCloudTableClient();

            CloudTable table = tableClient.GetTableReference("Studenttable");

            table.CreateIfNotExists();

        }

    }
}


We are going to work with the Studenttable. To represent an entity of the Studenttable let us create a StudentEntity class as in the following code snippet. Make sure to inherit the class from the TableEntity class.
 

public class StudentEntity : TableEntity

{

    public StudentEntity()

    {

       PartitionKey= "Student";

       RowKey= Guid.NewGuid().ToString();

    }

    public double RollNumber { get; set; }

    public string Name { get; set; }

    public string Grade { get; set; }

}


We can insert an entity in the table as in the following. The very first thing to do is to create an object of the entity class or in other words we need to create an entity object to be inserted. We can create that as in the following:

Figure6.jpg


Once the entity to be inserted has been created, the next thing we need to do is to create a TableOperationInsert. In the Insert function of TableOperation, pass an entity to be inserted. In this case studenttoInsert is the entity to be inserted, hence passed as a parameter. The last thing to do is to execute an operation by passing it as an input parameter of the Execute function on an instance of CloudTable.

Figure7.jpg

A Entity can be deleted by creating a delete operation and executing it; see:

Figure8.jpg


At last, let us consolidate all the discussions and the full source code for working with Windows Azure tables using Windows Azure Storage Client Library 2.0 as in the following:
 

using Microsoft.WindowsAzure.Storage;

using Microsoft.WindowsAzure.Storage.Auth;

using Microsoft.WindowsAzure.Storage.Table;

using System;

 

namespace AzureTableStorage

{

    class Program

    {

        static void Main(string[] args)

        {

            // Creating Storage Account

CloudStorageAccount account = new CloudStorageAccount(new StorageCredentials("youraccountname", "youraccountkey"),true);

 

            //Creating Table Client

            CloudTableClient tableClient = account.CreateCloudTableClient();

            //Creating Table

            CloudTable table = tableClient.GetTableReference("Studenttable");          

            table.CreateIfNotExists();

            //Creating Entity to insert

            var studenttoInsert = new StudentEntity

            {

                RollNumber = 1,

                Name = "Dhananjay Kumar",

                Grade = "Z"

            };

 

            // Inserting an entity

            TableOperation operationToInsert = TableOperation.Insert(studenttoInsert);

            table.Execute(operationToInsert);

            Console.WriteLine("Student Entity Inserted ");

            Console.ReadKey(true);

            //Deleting an  enitity

            TableOperation operationToDelete = TableOperation.Delete(studenttoInsert);

            table.Execute(operationToDelete);

            Console.WriteLine("Student Entity Deleted");

            Console.ReadKey(true);

        }

    }

}

I shall conclude this article by saying that we have learned how to perform basic operations on a Windows Azure table using the Windows Azure Storage Client Library 2.0. In future articles we will explore much more complex operations on Windows Azure tables. 

Up Next
    Ebook Download
    View all
    Learn
    View all