Objective
This article will give a walkthrough on how to perform a CRUD operation on Azure table.
Step 1
Creating the
Create a new Cloud Service Application. To create, File -> New -> Projects -> Cloud Services. Select ASP.Net Web Role. I am giving CRUDSample to the name of the project and CRUDWebRole to the name of the Web Role. After adding project, in solution explorer you can see two projects. One is project to publish in azure and other is simple asp.net project. In our case CRUDSample is project which contains configuration settings for azure and CRUDWebRole is asp.net project for development.
Step 2
Make sure below references are added CRUDWebRole projects.
System.Data.Service
System.Data.Service.Client
Microsoft.WindowsAzure.Diagnostics
Microsoft.WindowsAzure.ServiceRunTime
Microsoft.WindowsAzure.StorageClient.
Step 3
Creating Entity class
Now right click to asp.net project add a class which will model the table in azure. Give any name to this class. This class will act as entity table.
Inherit the class from
Microsoft.WindowsAzure.StorageClient.TableServiceEntity
- Add a default constructor
- In Default constructor initialize the Row key and Partition key
- Properties of the class define columns of the table.
Row Key and Partition key
These two keys uniquely define a table and row in azure storage.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Services; namespace CRUDWebRole {
{
{ RowKey = String.Format(“{0:10}_{1}”, (DateTime.MaxValue.Ticks – DateTime.Now.Ticks), Guid.NewGuid()); PartitionKey = “UST”; }
|
Step 4
Creating Context class.
Now right click on asp.net project and add a new class as context class. This class will contain function to perform all the CRUD operation. This class is inherited from TableServiceContext class.
- In default constructor passes the base address and storage credentials to base class.
-
Since TableServiceContext
class performs the operations against a class which is implementing IQueryable interface, so we will implement the interface for the entity class. - Just write simple LINQ for other CRUD functions.
Entire class is as below
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.StorageClient; namespace CRUDWebRole {
{
: base(baseAddress, credentials) { } public IQueryable<USTEntity> USTEntities {
public void UPDATEUSociate(USTClientEntity entity){ |
In above code I have used USTClientEntity class. This class is mapping of Entity class. This class is as below
USTClientEntity.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace CRUDWebRole {
{
}} |
Step 5
Working with connection strings
We have two options to work with
- Use local development center
- Use Azure table from Windows azure
Use Local Development center
This option will use the local storage created by development fabric controller when you install Azure SDK.
- Go to Azure Project. In this case it is , CRUDSample
- Click on Roles folder
- Then click on CRUDWebRole
- Click on setting tab and add connection string
- Click on Add Setting
- Make sure while adding connection string, you are choosing connection string from the drop down. Give any name to the string setting.
- Now click on left corner button
- To use local storage select the radio button Use Development Storage and click on OK button.
- To use AZURE table setting
Account Name: Give Azure storage account name
Account Key: Give Primary key from azure storage
Leave other setting as default and press ok.
For My purpose I have two connection strings in my application.
DiagnosticConnectionString
And
CrudString
Step 6
Creating table
- To create table in ASP.net project click on WebRole.cs class
-
And add below line of code to create class.
var account = CloudStorageAccount.FromConfigurationSetting(“CrudString”); CloudTableClient.CreateTablesFromModel(typeof(USTContext), account.TableEndpoint.AbsoluteUri, account.Credentials); |
Whole WebRole.Cs class will look like below ,
WebRole.cs
using System.Linq; using Microsoft.WindowsAzure.Diagnostics; using Microsoft.WindowsAzure.ServiceRuntime; using System.Data.Services.Client; using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.StorageClient; namespace CRUDWebRole { public class WebRole : RoleEntryPoint { configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
{
.Any((change) => (change.ConfigurationSettingName == configName))) {
{
} } };}); var account = CloudStorageAccount.FromConfigurationSetting(“CrudString”); {
{
e.Cancel = true; }}}} |
Here CrudString is name of the connection string.
Step 7
CRUD Operations
- To add a record
var account = CloudStorageAccount.FromConfigurationSetting(“CrudString”); var context = new USTClientEntity entity = new context.ADDUSociate(entity); |
- To update a record
entity = new USTClientEntity() {Id=ddlEmployees.SelectedItem.Text , Manager = txtManagerName.Text, Location = ddlCurrentLocation.SelectedItem.Text, Technology = ddlTechnology.SelectedItem.Text, Experience = ddlExperience.SelectedItem.Text }; var account = CloudStorageAccount.FromConfigurationSetting(“CrudString”); var context = new context.UPDATEUSociate(entity); |
- To Delete a record
var account = CloudStorageAccount.FromConfigurationSetting(“CrudString”);
{ context.DELUSociate(ddlEmployees.Text.ToString()); } |
-
To View all records
var account = CloudStorageAccount.FromConfigurationSetting(“CrudString”);
{
{ lst.Add(new } }
|
In all above cases drodownlist , Textboxes and GridView has been used as input and output control.
Now your application Is ready to run. Thanks for reading.
Leave a Reply