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

In this post we will take a look on performing basic operations on Windows Azure table using Windows Azure Storage Client Library 2.0.

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

image

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

image

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

image

After creating object of storage account, we need to create instance of CloudTabaleClient, We can create that as following,

image

Last step in creating Azure Table is to take a reference of CloudTable and create it if not exist. Studenttable is name of the table, we are intended to create here.

image

A Table can be deleted as following

image

Consolidating all the above discussion we can create a table with HTTPS connection using Windows Azure Storage Client Library 2.0 is as 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 Studenttable. To represent entity of Studenttable let us create StudentEntity class as given in following code snippet. Make sure to inherit the class from 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 table as following. Very first we need to create object of entity class or in other words need to create entity object to insert. We can create that as following,

image

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

image

Entity can be deleted by creating delete operation and executing it

image

In last let us consolidate all the discussions and full source code for working with Windows Azure table using Windows Azure Storage Client Library 2.0 is as 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 post by saying that we learnt how to perform basic operations on Windows Azure table using Windows Azure Storage Client Library 2.0. In further posts we will explore much more complex operations on Windows Azure table.

2 thoughts on “Basic Operations on Windows Azure Table in Windows Azure Storage in Storage Client Library 2.0

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s