Quantcast
Channel: Jean Paul's Blog » Azure
Viewing all articles
Browse latest Browse all 10

Create Table in Storage Account

$
0
0

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.

clip_image002

Step 2: Define the Entity

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

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

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)

{

StorageCredentialsAccountAndKey accountAndKey = new

StorageCredentialsAccountAndKey("youraccount", "key");

CloudStorageAccount account = new CloudStorageAccount(accountAndKey, true);

CloudTableClient client = account.CreateCloudTableClient();

client.CreateTableIfNotExist("Contact");

TableServiceContext tableContext = new

TableServiceContext(account.TableEndpoint.ToString(),

account.Credentials);

Contact contact = new Contact();

contact.PartitionKey = "1";

contact.RowKey = "1";

contact.Name = "Contact1";

contact.Address = "Address1";

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 Explorerto see the new entity inserted as shown below.

clip_image004

Summary

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



Viewing all articles
Browse latest Browse all 10

Trending Articles