Presentation is loading. Please wait.

Presentation is loading. Please wait.

Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows.

Similar presentations


Presentation on theme: "Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows."— Presentation transcript:

1 Table Storage

2 Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows Azure Storage: using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.Storage; using Microsoft.WindowsAzure.Storage.Auth; using Microsoft.WindowsAzure.Storage.Table; Microsoft.WindowsAzure.Storage.dll assembly is to be added.

3 Access Table storage Retrieving your connection string use the CloudConfigurationManager type to retrieve your storage connection string and storage account information from the Windows Azure service configuration: CloudStorageAccount storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("S torageConnectionString"));

4 Access Table storage A CloudTableClient type allows you to retrieve objects that represent tables stored within the Table Storage Service. The following code creates a CloudTableClient object using the storage account object we retrieved above: CloudTableClient tableClient = storageAccount.CreateCloudTableClient() ;

5 Access Table storage Create a Table use a CloudTableClient object to get a reference to the Table you want to use. You can create the Table if it doesn't exist:

6 Access Table storage Create a Table... // Retrieve a reference to a Table. CloudTable table=tableClient.GetTableReference(“co ntacts"); // Create the table if it doesn‘t alreadyexist. table.CreateIfNotExists();

7 Access Table storage Entities map to C# objects using a custom class derived from TableEntity. To add an entity to a table, create a class that defines the properties of the entity. For any property that should be stored in the table service, the property must be a public property of a supported type that exposes both get and set. Also, the entity type must expose a parameter-less constructor.

8 Access Table storage Entities with the same partition key can be queried faster than those with different partition keys, but using diverse partition keys allows for greater parallel operation scalability. The following code defines an entity class that uses the contact type as the row key and phone number as the partition key. Together, an entity's partition and row key uniquely identify the entity in the table.

9 Access Table storage public class ContactEntity : TableEntity { public ContactEntity(string contactType string lastName, string firstName,int contactNo) { this.PartitionKey = contactTYpe; this.RowKey = contactNo; this.firstName=firstName; this.lastName=lastName; } public ContactEntity() { } public string lastName{get;set;} public string firstName{get;set;} }

10 Access Table storage To create an entity // Create a new contact entity. ContactEntity contact1 = new ContactEntity(“friends”,"Harp“,"Walter“, 4255550101);

11 Access Table storage To create an entity // Create the TableOperation that inserts the contact entity. TableOperation insertOperation = TableOperation.Insert(contact1); // Execute the insert operation. table.Execute(insertOperation);

12 Access Table storage To retrieve an entity from a table use a a TableQuery object. TableQuery query = new TableQuery ().Where(Table Query.GenerateFilterCondition("Partitio nKey", QueryComparisons.Equal, “friends"));

13 Access Table storage To retrieve an entity from a table use a a TableQuery object. foreach (ContactEntity entity in table.ExecuteQuery(query)) { Console.WriteLine("{0}, {1}\t{2}\t{3}", entity.PartitionKey, entity.RowKey, entity.firstName, entity.lastName); }

14 Access Table storage Replace an entity To update an entity, retrieve it from the table service, modify the entity object, and then save the changes back to the table service. The following code changes an existing contact's phone number. Instead of calling Insert, this code uses Replace. This causes the entity to be fully replaced on the server, unless the entity on the server has changed since it was retrieved, in which case the operation will fail.

15 Access Table storage Replace an entity This failure is to prevent your application from inadvertently overwriting a change made between the retrieval and update by another component of your application. The proper handling of this failure is to retrieve the entity again, make your changes (if still valid), and then perform another Replace operation.

16 Access Table storage Replace an entity // Create a retrieve operation that takes a customer entity. TableOperation retrieveOperation = TableOperation.Retrieve ( “ ", “ "); // Execute the operation. TableResult retrievedResult = table.Execute(retrieveOperation);

17 Access Table storage Replace an entity // Assign the result to a CustomerEntity object. ContactEntity updateEntity = (ContactEntity)retrievedResult.Result;

18 Access Table storage Replace an entity if (updateEntity != null) { // Change the phone number. updateEntity.RowKey = 4255550105; // Create the Replace TableOperation TableOperation updateOperation = TableOperation.Replace(updateEntity); // Execute the operation. table.Execute(updateOperation); Console.WriteLine("Entity updated."); } else Console.WriteLine("Entity could not be retrieved.");

19 Access Table storage Insert-or-replace an entity Replace operations will fail if the entity has been changed since it was retrieved from the server. Furthermore, you must retrieve the entity from the server first in order for the Replace to be successful. Sometimes, however, you don't know if the entity exists on the server and the current values stored in it are irrelevant - your update should overwrite them all.

20 Access Table storage Insert-or-replace an entity To accomplish this, you would use an InsertOrReplace operation. This operation inserts the entity if it doesn't exist, or replaces it if it does, regardless of when the last update was made. In the following code example, the contact entity for Ben Smith is still retrieved, but it is then saved back to the server using InsertOrReplace. Any updates made to the entity between the retrieval and update operation will be overwritten.

21 Access Table storage Replace an entity TableOperation insertOrReplaceOperation = TableOperation.InsertOrReplace(updateEn tity); // Execute the operation. table.Execute(updateOperation);

22 Access Table storage Delete an entity You can easily delete an entity after you have retrieved it, using the same pattern shown for updating an entity. The following code retrieves and deletes a customer entity.

23 Access Table storage Delete an entity // Create the Delete TableOperation. TableOperation deleteOperation = TableOperation.Delete(deleteEntity); // Execute the operation. table.Execute(deleteOperation);

24 Access Table storage Delete a table Finally, the following code example deletes a table from a storage account. A table which has been deleted will be unavailable to be recreated for a period of time following the deletion. // Delete the table it if exists. table.DeleteIfExists();


Download ppt "Table Storage. Access Table storage Add the following namespace declarations to the top of any C# file in which you wish to programmatically access Windows."

Similar presentations


Ads by Google