Using Windows Azure table storage

The Azure Table service, provides a semi-structured storage in the form of tables  that contain a collection of entities, these entities have primary keys and set of properties. A property is a name, typed-value pair.

Every entity in table storage has two key properties, Partition Key and the Row key which together uniquely identify each entity in the table.

The Table Service API is compliant with REST API provided by WCF Data Services. In order to use the WCF Data Services Client Library to access data in table storage, you need to create a context class that derives from TableServiceContext, which itself derives from DataServiceContext in WCF Data Services.

So to get started lets create a new project with Visual Studio with a web role.

Next we need to create a new project for the schema classes. To do this click on File –> Add –> New Project.

  • Choose Class Library and give it a name I named mine LogSheet_Data
  • Delete the default class file generated by the class library template.
  • Add a reference to System.Data.Services.Client.
  • Add another reference to Microsoft.WindowsAzure.StorageClient 
  • Now we are ready to define its schema. to do this add a new class to your project, I am going to name mine LogSheetEntry
    • Add the namespace declaration to import the types from the two assemblies

using Microsoft.WindowsAzure.StorageClient;

    • Update the declaration of the LogSheetEntry class to make it public and derive from the the TableServiceEntity class

public class LogSheetEntry
    : Microsoft.WindowsAzure.StorageClient.TableServiceEntity
    {
    }

  • TableServiceEntity is a class found in the Storage Client API. This class defines the PartititionKey, RowKey and TimeStamp system properties required by every entity stored in a Windows Azure table.
    Together, the PartitionKey and RowKey define the DataServiceKey that uniquely identifies every entity within a table.
    • Add a default constructor then we need to initialize its PartitionKey and Row Key.

 

public LogSheetEntry()
{           
    PartitionKey = DateTime.UtcNow.ToString("MMddyyyy");
 
    RowKey = string.Format("{0:10}_{1}", DateTime.MaxValue.Ticks - DateTime.Now.Ticks, Guid.NewGuid());
}

    • To complete add the properties

public string Facility { get; set; }
public string AccountNum { get; set; }

    • Save
  • Next, you need to create the context class required to access the LogSheet table using WCF Data Services. To do this, in Solution Explorer, right-click the LogSheet_Data project, point to Add and select Class. In the Add New Item dialog, set the Name to LogSheetDataContext.cs   and click Add.
  • In the new class file, update the declaration of the new class to make it public and inherit the TableServiceContext class.

public class LogSheetDataContext
  : Microsoft.WindowsAzure.StorageClient.TableServiceContext

  • Now add a default constructor to initialize the base class with storage account information

public GuestBookDataContext(string baseAddress, Microsoft.WindowsAzure.StorageCredentials credentials)
    : base(baseAddress, credentials)
{ }

  • Add a property to the LogSheetDataContext class to expose the LogSheetEntry

public IQueryable<LogSheetEntry> LogSheetEntry
{
  get
  {
    return this.CreateQuery<LogSheetEntry>("LogSheetEntry");
  }
}

  • Finally, you need to implement an object that can be bound to data controls in ASP.NET. In Solution Explorer, right-click LogSheet_Data, point to Add, and select Class. In the Add New Item dialog, set the name to LogSheetDataSource.cs
  • In the new class file, add the following namespace declarations to import the types contained in the Microsoft.WindowsAzure and Microsoft.WindowsAzure.StorageClient namespaces.

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

  • Make the class public and define member fields for the data context and storage account information

public  class LogSheetDataSource
  {
      private static CloudStorageAccount storageAccount;
      private LogSheetDataContext context;
  }

  • Now , add a static constructor That will create the tables from the LogSheetDataContext class

   static LogSheetDataSource()
{
  storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
  CloudTableClient.CreateTablesFromModel(
      typeof(LogSheetDataContext),
      storageAccount.TableEndpoint.AbsoluteUri,
      storageAccount.Credentials);
}

  • add a default constructor to initialize the data context class used to access table storage.

public LogSheetDataSource()
{
    this.context = new LogSheetDataContext(storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials);
  this.context.RetryPolicy = RetryPolicies.Retry(3, TimeSpan.FromSeconds(1));
}

  • Next we will need a method that will return the contents of LogSheetEntry table

public IEnumerable<LogSheetEntry> GetLogSheetEntries()
{
    var results = from g in this.context.LogSheetEntry
                  where g.PartitionKey == DateTime.UtcNow.ToString("MMddyyyy")
                  select g;
    return results;
}

  • We also are gong to need a method that will insert new entries

public void AddLogSheetEntry(LogSheetEntry newItem)
{
    this.context.AddObject("LogSheetEntry", newItem);
    this.context.SaveChanges();
}

  • You guessed it we will need a method for an update, for simplicity, we are going to change the the facility only at this time.

public void UpdateImageThumbnail(string partitionKey, string rowKey, string Facility)
{
    var results = from g in this.context.LogSheetEntry
                  where g.PartitionKey == partitionKey && g.RowKey == rowKey
                  select g;
    var entry = results.FirstOrDefault<LogSheetEntry>();
    entry.Facility = Facility;
    this.context.UpdateObject(entry);
    this.context.SaveChanges();
}

  • Next steps is Creating a Web Role to Display the Time Sheet Entry and process User input, which is going to be in my next blog entry.

 

Print | posted on Tuesday, November 1, 2011 2:40 PM

Comments on this post

# re: Using Windows Azure table storage

Requesting Gravatar...
my friend I have a question to you. My mothers laptop ASUS that runs on Windows 7 has a problem and they say that the whole system needs to reinstalled. What should I do?
Left by nowoczesne projekty domow on Dec 31, 2011 1:25 PM

# re: Using Windows Azure table storage

Requesting Gravatar...
Great explanation on the Time sheet entry coding, thanks for sharing.
Left by write my essay for free on Jan 12, 2012 10:03 AM

# re: Using Windows Azure table storage

Requesting Gravatar...
While using some of the best operating systems like windows we found several options for getting a fair benefits.
Left by harry potter world on Mar 05, 2012 1:23 AM

# re: Using Windows Azure table storage

Requesting Gravatar...
It looks very effective. I must try to use it. Thanks for sharing.

Bed Bug Control Toronto
Left by Pest Control Toronto on Mar 11, 2012 1:26 PM

# re: Using Windows Azure table storage

Requesting Gravatar...
thanks for sharing!
Left by Mobile Dating Application on Apr 01, 2012 9:11 AM

# re: Using Windows Azure table storage

Requesting Gravatar...
Good luck to the author! all the best!
Left by Felt Ball Rug on May 07, 2012 5:50 AM

# re: Using Windows Azure table storage

Requesting Gravatar...
Very detailed post. I had great time learning - Thanks for sharing.
Left by Mike R melbourne on May 15, 2012 9:04 AM

# re: Using Windows Azure table storage

Requesting Gravatar...
Nice one indeed
Left by Photoshop Download free on Jan 08, 2013 5:53 AM

# re: Using Windows Azure table storage buzz

Requesting Gravatar...
Article interessant, un grand merci à vous et surtout à bientôt dans de nouvelle aventure
Left by buzz on Jan 09, 2013 10:18 AM

# re: Using Windows Azure table storage

Requesting Gravatar...
Otimo artigo.
Left by lembrancinhas gravatinhas on Feb 24, 2013 5:19 PM

# re: Using Windows Azure table storage

Requesting Gravatar...
Usted necesita encontrar un buen forro y relleno de una chaqueta.
Left by chaquetas belstaff outlet on Jan 05, 2014 9:07 PM

# re: Using Windows Azure table storage

Requesting Gravatar...
Select the correct cologne is a daunting task, shopping. BURBERRY cologne to provide, in order to satisfy the taste of almost anyone - from safe bets, such as BURBERRY BRIT, give more unconventional options, such as BURBERRY summer or discount men's cologne. Burberry cologne from light to B, from almost any occasion.
Left by Tracy Toler Wilson on Jan 09, 2014 10:44 PM

# re: Using Windows Azure table storage

Requesting Gravatar...
Burberry brand is Chang Ying said very high-end luxury goods. Burberry are well known, the outdoor clothing, but these days much better known as the classic design of the handbag. If you walk into a burberry store, is willing to pay an average of about $600 - $1000 fashion designer bags. For this reason, for a real burberry handbags online shopping is all are a bargain.
http://www.modknow.net
Left by Tracy Toler on Jan 09, 2014 10:45 PM

# Heat Bed Bugs Phoenix

Requesting Gravatar...
NaturZone Pest Control offers reliable bed bug services for commercial and residential customers in Phoenix AZ Scottsdale,Tempe,Glendale,Gilbert,Mesa,Avondale. http://naturzonebedbugs.com
Left by naturzone on Mar 12, 2014 11:05 AM

Your comment:

 (will show your gravatar)