Windows Azure - Create Table in Storage Account

In this article we can explore the table creation in storage account.

As a refresh, I would like to repeat that there are 3 types in Azure Storage.

  • Blob
  • Table
  • Queue

The Blob creation was explored in the previous article.  Table creation will be explored here.

Concepts in Table

Following are the key concepts in table.

  • Tables allow structure data storage
  • There can be 0..n tables in a storage account
  • Table store data as a collection of entities
  • Entity have a primary key and properties as key value pair

Note: The table we discuss here is not entirely same as our database table.  The database table will be discussed in SQL Azure.  In Blob Service, the data is stored in containers.  But in Table Service, data is stored in tables.

The steps involved in creating a table are following:

Step 1: Create new project

As always create a new azure project and add a web role into it.  Now add reference to the StorageClient dll file.

d1.gif

Step 2: Define the Entity

Now we need to define the entity with the required properties, create a new class, derive it from TableServiceEntity and define the following properties in it.

Our entity class is  derived from TableServiceEntity because this class takes care of the key properties like PartitionKey, RowKey and necessary attributes.

The class definition is given below:

public class Contact : TableServiceEntity

{
    public string Name
    {

        get;

        set;

    }

    public string Address

    {

        get;

        set;

    }

}    

Step 3: Code Part

Now we can write the code to do the following activities:

  • Do account authentication
  • Create Table client
  • Create table in account
  • Add the new entity to tabl

The following code performs the following:

protected void Page_Load(object sender, EventArgs e)
{

    // Authenticate
    StorageCredentialsAccountAndKey accountAndKey = new StorageCredentialsAccountAndKey("youraccount", "key");
    CloudStorageAccount account = new CloudStorageAccount(accountAndKey, true);
   
CloudTableClient client = account.CreateCloudTableClient();
    client.CreateTableIfNotExist("Contact");

    // Create table context
    TableServiceContext tableContext = new TableServiceContext(account.TableEndpoint.ToString(), account.Credentials);

    // Create entity

    Contact contact = new Contact();

    contact.PartitionKey = "1";
    contact.RowKey = "1";
    contact.Name = "Contact1";
    contact.Address = "Address1";            

    // Add entity to table and save the changes

    tableContext.AddObject("Contact", contact);
    tableContext.SaveChanges();
}

The account name and key can be obtained as explained in the previous article.

Note: We are setting the PartitionKey and RowKey as "1" for demo purposes.  The PartitionKey represents the partition identifier where  the table is stored.  The RowKey should be the unique identifier to the entity. More Info

The classes involved for resembles the ADO.NET Entity Framework classes.

Step 4: Viewing the entity inserted.

You can use the Server Explorer to see the new entity inserted as shown below.

d2.gif

Summary

In this article we have seen how to define a table and insert entity into it. 

Up Next
    Ebook Download
    View all
    TOP Azure
    Read by 1 people
    Download Now!
    Learn
    View all