Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Azure

Developing with the Table service

3/12/2011 10:35:57 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
Now that you have an understanding of how data is stored in the Table service, it’s time to develop a web application that can use it. We defined an entity for storing the Hawaiian shirt product, and we looked at how it would be stored in the Table service. Here you’ll build a new application that will manage the product inventory for the Hawaiian Shirt Shop website.

1. Creating a project

Create a new Cloud Service web role project called ShirtManagement. If you need a refresher on how to set up your development environment or how to create a web role project.

Like the other storage services, communication with the Table service occurs through the REST API . Although you can use this API directly, you’re likely to be more productive using the StorageClient library provided in the Windows Azure SDK.

Whenever you create a new Cloud Service project, this library will be automatically referenced. But if you’re building a brand new class library or migrating an existing project, you can reference the following storage client assembly manually:

  • Microsoft.WindowsAzure.StorageClient

In addition, you’ll need to reference the ADO.NET Data Services assemblies:

  • System.Data.Services

  • System.Data.Services.Client

ADO.NET Data Services

The Table service exposes its HTTP REST APIs through its implementation of the ADO.NET Data Services framework. By using this framework, we can utilize the ADO.NET Data Services client assemblies to communicate with the service rather than having to develop or use a custom REST wrapper.

Because ADO.NET Data Services is used by the storage client SDK, you’ll need to reference those assemblies too.


Now that you’ve set up your project, let’s look at how you can add the Product entity class to the project.

2. Defining an entity

Before you create your product-management web page, you need to create an entity in the web project. At this stage, we’ll just show you how to add the entity directly to the web page.

To keep this example simple, we’ll just store the shirt name and description, as before. Add a new class to your web project named Product.cs and define the class as shown in the following listing.

Listing 1. Product entity
public class Product : TableServiceEntity
{
public string Name { get; set; }
public string Description { get; set; }
}

In listing 1, the Product class inherits from the TableServiceEntity base class (Microsoft.WindowsAzure.TableService.TableServiceEntity). This base class contains the three properties required by all table storage entities:

  • Timestamp

  • PartitionKey

  • RowKey

Now that you’ve set up your project and defined your entity, you need to create a table to store the entity in. The same method can be used in both the development and live environments.

3. Creating a table

The simplest method of creating a table is to create a PowerShell script or to use one of the many storage clients that are available. We’ll use Azure Storage Explorer, which you can download from CodePlex: http://azurestorageexplorer.codeplex.com/.

In this section, we’ll look at how to create a table in two ways: using Azure Storage Explorer and using code.

Creating a Table Using the Azure Storage Explorer

Once you have downloaded and fired up Azure Storage Explorer, it will automatically connect you to your development storage account as long as your local development storage service is running.

To create a new table in your storage account, all you need to is select Table > New Table and enter the name of your table (Products, in this case). Figure 1 shows the newly created Products table in Azure Storage Explorer.

Figure 1. The Azure Storage Explorer showing the newly created Products table


Although using a tool such as Azure Storage Explorer is probably the easiest method of creating a new table, you may wish to do this manually in C#.

Creating a Table in Code

In this example, you’ll manually create a console application that will create a new table in the storage account when it’s run. Although we’ll have you use a console application in this example, you could easily use a web application, Windows Forms application, or Windows Presentation Foundation application. The deployment application doesn’t need to be a cloud application (web or worker role); it can be a standard application that you run locally.

To create the application, perform the following steps:

1.
Create a console application targeting .NET 3.5.

2.
Add a reference to System.Data.Services.

3.
Add a reference to System.Data.Services.Client.

4.
Add a reference to Microsoft.WindowsAzure.StorageClient.

5.
Add an app.config or web.config entry with your storage account credentials.

6.
Add the following code to create the table:

CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
{
configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
});

var storageAccount =

CloudStorageAccount.FromConfigurationSetting("DataConnectionString");

CloudTableClient tableClient =
storageAccount.CreateCloudTableClient();

tableClient.CreateTableIfNotExist("Products");


The code added in step 6 retrieves storage account information from the app.config and then calls the CreateTableIfNotExist method from the CloudTableClient object, passing in the name of the table to create (Products).

Deploying to live

The code used to create a new table will work not only on your development storage account, but will also work against the live system. All you need to do to make this code work against the live system is to change the DataConnectionString configuration setting to your live account.


Now that you know how to create a table both in the live system and in development storage, it’s worth taking a quick peek at how this is implemented in the development storage backing store. Figure 2 shows how tables are represented in the development storage SQL Server database.

Figure 2. How tables are represented in the development storage SQL Server database


As you can see in figure 2, the SQL Server database that stores the entities is pretty flexible. The TableContainer table keeps a list of all the tables stored in the development storage account. Because you can create tables dynamically, any new table created will contain a new entry in this table.

Each row in the TableRow table in figure 2 stores a serialized version of the entity. As you can see from this table definition, the only fixed data that’s stored in this table is AccountName, TableName, PartitionKey, RowKey, and TimeStamp. All other properties are stored in the Data column. All other properties are stored in the Data column. As you can see, the actual development storage schema relates to the logical representation that you saw in table 4.

Now that you’ve seen how tables are represented in development storage, let’s look at how you can start working with your entities.

Other -----------------
- Partitioning data across lots of servers : Partitioning the storage account & Partitioning tables
- Modifying an entity to work with the Table service
- How we’d normally represent entities outside of Azure
- A brief overview of the Table service
- BLOBs : Setting shared access permissions
- Enterprise Service Bus with BizTalk Server and Windows Azure : Distributed and Scalable ESB Architecture
- Enterprise Service Bus with BizTalk Server and Windows Azure : The ESB Toolkit
- Enterprise Service Bus with BizTalk Server and Windows Azure : Integration with BizTalk
- Copying BLOBs - Copying files via the StorageClient library
- Using local storage with BLOB storage (part 3) - Improving your handler to check the last modified time
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server