What you want to see on your favorite website?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 3807 |  Welcome, Guest!   Register  Login
Home > Articles > Azure > 9 simple steps to run your first Azure Table Program

9 simple steps to run your first Azure Table Program

Article posted by Questpond on 1/11/2010 | Views: 5899 | Category: Azure | Level: Beginner red flag


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.

Download


 Download source code for 9 simple steps to run your first Azure Table Program


9 simple steps to run your first Azure Table Program

 

Introduction

What will we do in this article?

Step 1:- Ensure you have things at place

Step 2:- Create a web role project

Step 3:- Specify the connection string

Step 4:- Reference namespaces and create classes

Step 5:- Define partition and row key

Step 6:- Create your ‘datacontext’ class

Step 8:- Code your client

Step 9:- Run your application
 

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 complete fresher and like me you can download my two azure basic videos which explain what azure is all about Azure FAQ Part 1 :-  Video1  Azure FAQ Part 2 :- Video2.
Please feel free to download my free 500 question and answer eBook which covers .NET , ASP.NET , SQL Server , WCF , WPF , WWF , Silver light , Azure @ http://tinyurl.com/4nvp9t .
 

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 at place
 

In case you are a complete fresher to Azure, please ensure you have all the pre-requisite at place. You can read the below article to get the basic prerequisite http://www.dotnetfunda.com/articles/article758-simple-5-steps-to-run-your-first-azure-program-.aspx  .
 

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 3rd step is to specify the connection string where your table source is currently. So expand the roles folder , right click on webroletable and select properties as shown in the below figure.
 

You will be then popped up with a setting tab. 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 you 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 reference to ‘System.Data.Services.Client’ dll.

Once the dlls are referred, let’s refer 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 in to 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 call’s the ‘AddObject’ method of the data context to insert the customer entity data in to 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 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 a 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 in to 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 in to the table.
 

//Loop through the records to see if the customer entity is inserted in the tabless
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.
 

Source code
 

You can get the source code from top of this article.
 

 

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:0 year(s)
Home page:http://www.questpond.com
Member since:Wednesday, September 03, 2008
Level:Starter
Status: [PanelMember] [Member] [Microsoft_MVP] [MVP] [Administrator]
Biography:

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

>> Write Response - Respond to this post and get points
Related Posts

In this article we will look in to 5 basic steps which will help us to run our first azure program. In this article we will understand how to create a simple web role application and while doing the same we will understand some development concepts of Azure.

Different people have different obsessions and I have this stupid obsession of writing articles in FAQ formats :-) . The more I try to write articles in normal format I end up with a FAQ. My only thought process of writing articles in FAQ format is that we end up talking to the point rather than talking about trees and rivers , many may disagree.

There is a buzz around cloud adoption which promises capacity-on-demand and elasticity. Windows Azure is the answer to cloud from Microsoft. Let us discuss about Azure in a series of articles.

Azure queues help to communicate data between web role and worker role. Web roles are nothing but web application which can be accessed by the end browser. Worker roles are back ground processes which run in the azure system and they do not take direct request from the web. So if we want to pass data to worker roles we need to use Queues. Queues are temporary storage which acts like a bridge between web role and worker role. So you can post data to web role , web role posts data to queues and worker role picks the data from queues to do further processing.

Access Control provides an easy way to provide identity and access control to web applications and services, while integrating with standards-based identity providers, including enterprise directories such as Active Directory, and web identities such as Windows Live ID, Google, Yahoo! and Facebook.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/20/2013 11:48:33 PM