Share via


Windows Azure Table Storage

Windows Azure Table has changed quite a bit since SDK 1.x. So there are buch of new approaches and ways to handle them

First we need to install WindowsAzure.Storage from https://nuget.org

image

Now have the entity

 public class Employee : TableEntity
{
    public Employee()
    {
        this.PartitionKey = "emp";
        this.RowKey = Guid.NewGuid().ToString();
    }
    public string FullName { get; set; }
}

Then a simple ASP.NET page which has textbox, button and a grid would look like

 public partial class _Default : Page
{
    //Sample Connection String

    CloudStorageAccount csa =
            CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["DataConnection"].ConnectionString);
    string tableName = "employee";
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            //ShowData();
        }
    }

    private void SaveData()
    {
        CloudTableClient tableClient = csa.CreateCloudTableClient();

        CloudTable table = tableClient.GetTableReference(tableName);
        table.CreateIfNotExists();

        Employee empData = new Employee() { FullName = txtName.Text };

        TableOperation insertOps = TableOperation.Insert(empData);

        table.Execute(insertOps);
    }

    private void ShowData()
    {
        CloudTableClient tableClient = csa.CreateCloudTableClient();

        CloudTable table = tableClient.GetTableReference(tableName);
        table.CreateIfNotExists();

        TableQuery<Employee> query = new TableQuery<Employee>().Where(
            TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "emp"));

        var allEmps = table.ExecuteQuery(query);

        grdEmployee.DataSource = allEmps;
        grdEmployee.DataBind();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        SaveData();
        ShowData();
    }        
}

Namoskar!!!