In the previous post I showed how to use basic PowerShell commands to manage azure tables. In this post I am going to show how to create/delete tables using C# and Azure Storage Library.
Refer to previous posts to get started with Azure Storage Table Service.
Consider the following example:
var tableClient = storageAccount.CreateCloudTableClient(); var table = tableClient.GetTableReference("table1"); table.CreateIfNotExists(); table.CreateIfNotExists();
The CreateIfNotExists method will create a table if it doesn't exist. It is safe to call this method multiple times to make sure that the table exists.
On the other hand, the following code throws an exception:
var tableClient = storageAccount.CreateCloudTableClient(); var table = tableClient.GetTableReference("table1"); table.Create(); table.Create();
It will throw the following exception:
The remote server returned an error: (409) Conflict.
A similar behavior is implemented for Delete operation. So the following code works fine:
And this code throws an exception:
The remote server returned an error: (404) Not Found.
You can check if table exists or not using the corresponding method Exists, but we don't need it for our simple scenario.
Unfortunately, there is a chance that CreateIfNotExists will throw (409) Conflict because when you delete the table, it is actually marked for deletion and is actually deleted through a background process (much like garbage collection). If you try to create a table between these two steps, you will get the error. There are many ways to handle this situation, one of them is to simply wait for awhile before crating the table. The other is to use on of retry policies.
In this post I have described how one can create and delete Azure Tables. HTTP error codes are self explanatory, if you try to create a table that already exists - 409 (Conflict) will be returned. If you try to delete a table that doesn't exist anymore - 404 (Not Found) will be returned. In the next post I will show how to add a single entity to Azure Table.