Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

9 Simple Steps to Run Your First Azure Table Program

, 26 Feb 2014
Rate this:
Please Sign up or sign in to vote.
9 simple steps to run your first Azure Table Program

Table of Contents

Introduction

Azure has provided 4 kinds of data storages blobs, tables, queues and SQL Azure. In this section, we will see how to insert a simple customer record with code and name property in Azure tables.

In case you are a complete fresher like me, you can download my two Azure basic videos which explain what Azure is all about:

Please feel free to download my free 600+ question and answer eBook which covers .NET, ASP.NET, SQL Server, WCF, WPF, WWF, Silverlight, Azure click here

What Will We Do in this Article?

We will create a simple customer entity with customer code and customer name and add the same to Azure tables and display the same on a web role application.

Step 1: Ensure You Have Things In Place

In case you are a complete fresher to Azure, please ensure you have all the prerequisites in place. You can read this article to get the basic prerequisites.

Step 2: Create a Web Role Project

The next step is to select the cloud service template, add the web role project and create your solution.

Step 3: Specify the Connection String

The third step is to specify the connection string where your table source is currently at. So expand the roles folder, right click on webroletable and select properties as shown in the below figure: 
 

A setting tab will then pop up. Select the settings section, add a new setting, give a name to your connection string and select type as ‘connectionstring’.

We also need to specify where the storage location is, so select the value and select ‘Use development storage’ as shown in the below figure. Development storage means your local PC currently where your Azure fabric is installed.

If you open the ‘ServiceConfiguration.cscfg’ file, you can see the setting added to the file.

Step 4: Reference Namespaces and Create Classes

In order to do Azure storage operation, we need to add a reference to ‘System.Data.Services.Client’ DLL.

Once the DLLs are referred, let’s refer to the namespaces in our code as shown below. Currently we will store a customer record with customer code and customer name in tables. So for that, we need to define a simple customer class with ‘clsCustomer’. This class needs to inherit from ‘TableServiceEntity’ class as shown in the below figure.

The second class which we need to create is the data context class. The data context class will take the entity class and enter the same in tables. You can see in the below figure we have created one more class ‘clsCustomerDataContext’.

Step 5: Define Partition and Row Key

The next step is to define the properties of the customer class. In the below figure, we have defined two properties in the customer class - customer code and customer name.

Every row in the table needs to be defined with a partition key and a unique row key. In the constructor, we have initialized the partition key with a text “Customers” and the unique key is set to the current date time tick count.

Step 6: Create Your ‘datacontext’ Class

The next step is to create your data context class which will insert the customer entity into Azure table storage. Below is the code snippet of the data context class.

The first noticeable thing is the constructor which takes in location of the credentials. The second is the ‘Iqueryable’ interface which is used by the cloud service to create tables in Azure cloud service.

In the same data context, we have created an ‘AddCustomer’ method which takes in the customer entity object and calls the ‘AddObject’ method of the data context to insert the customer entity data into Azure tables.

Step 7: Create the Table Structure on the ‘onstart’

The next step is to create the table on the ‘onstart’ of the web role.

So open ‘webrole.cs’ file and put the below code on the ‘onstart’ event. The last code enclosed in curly brackets gets the configuration and creates the table's structure.

Step 8: Code your Client

The final thing is to code the client. So below is the UI / ASPX file which we have created to insert the table entity values.

On the button click, we need to consume the data context and the entity class.

So the first step is to get the configuration setting of the data connection.

// Gets the connection string
var customer = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");

The next step is to pass these credentials to the data context class and create an object of the same.

// Create the customer datacontext object
var customerContext = new clsCustomerDataContext
	(customer.TableEndpoint.ToString(), customer.Credentials);

Flourish the entity object with data and pass it to the data context class to add the same into tables. 

// Create the entity object
clsCustomer objCustomer = new clsCustomer();
objCustomer.CustomerCode = txtCustomerCode.Text;
objCustomer.CustomerName = txtCustomerName.Text;
// Pass the entity object to the datacontext
customerContext.AddCustomer(objCustomer);

Finally we loop through the context customer entity collection to see if the customer is added into the table. 

//Loop through the records to see if the customer entity is inserted in the tables
foreach (clsCustomer obj in customerContext.Customers)
{
Response.Write(obj.CustomerCode + " " + obj.CustomerName + "<br>");
}

Step 9: Run your Application

It’s time to enjoy your hard work, so run the application and enjoy your success.

History

  • 10th January, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shivprasad koirala
Architect http://www.questpond.com
India India

I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small
E-learning company in India. We are very much active in making training videos ,
writing books and corporate trainings. Do visit my site for 
.NET, C# , design pattern , WCF , Silverlight
, LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server  training 
and Interview questions and answers


Comments and Discussions

 
GeneralMy vote of 5 PinmvpKanasz Robert28-Sep-12 5:43 
QuestionNice Article - One thing to be revised PinmemberParameshwaran G29-Oct-11 23:56 
Hi Shiv
 
Very nice article Smile | :) Good way to start off on using Table Storage. One revision maybe required. this regards the code currently in the Webrole on_start method. Starting with Azure SDK 1.3 this needs to be re-written into Global.asax in the On_start method.
CloudStorageAccount.SetConfigurationSettingPublisher(
       (configName, configSettingPublisher) =>
       {
           var connectionString =
               RoleEnvironment.GetConfigurationSettingValue(configName);
           configSettingPublisher(connectionString);
       }
       );
 

 
Request you to update the same..
 
Thanks again,
Param

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 26 Feb 2014
Article Copyright 2010 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid