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.

About these ads

Fetching name of all tables in Windows Azure Storage

To list all the table name from Windows Azure storage account, you need to call ListTable() function of CloudTableClient class. To start with first you need to add below namespaces.

image

Then create a storage account as below,

clip_image001

Make sure to put your own connection string to azure storage to parse. Next you need to create instance of table client

clip_image003

Once tableclient is created you can list all the tables by making call to ListTables() method.

clip_image004

For your reference full source code to fetch all the tables name from Windows Azure Storage account is given below,


using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure;
namespace ConsoleClient
{
class Program
{
static void Main(string[] args)
{

string connectionString = "DefaultEndpointsProtocol=https;AccountName=abc;AccountKey=jsjdsfjgdsfj09===";
var TablesName = GetTablesNameForAzureSubscription(connectionString);
foreach (var r in TablesName)
{
Console.WriteLine(r.ToString());
}
Console.ReadKey(true);
}

private static List<string> GetTablesNameForAzureSubscription(string connectionString)
{
CloudStorageAccount account =CloudStorageAccount
.Parse(connectionString);

CloudTableClient tableClient = new CloudTableClient
(account.TableEndpoint.ToString(),
account.Credentials);

var result = tableClient.ListTables();

return result.ToList();
}

&nbsp;

On running of above code you should be getting the table name listed for the account mentioned in connection string. In this case I have three tables listed to the account as below,

clip_image001[6]

In this way you can list all the tables name from Windows Azure Storage Account. I hope this post is useful. Thanks for reading  Smile

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.