locked
How to use Azure Storage Table as database on my ASP.Net site RRS feed

  • Question

  • User-775831949 posted

    I have created Azure Storage account and have installed desktop Storage Explorer showing my storage account.

    I want to create a table in the Storage account but I am totally lost

    For example, I would like to create a table with this structure:-

    ID, Name, Dept

    then add 2 records:-

    1, Steve, Marketing
    2, Mary, Finance

    How to create this table and insert the 2 records above ?

    After that, how can I get this 2 rows of data into my sqldatasource ?

    What is the Connectionstring I can use in web.config ?

    Thanks a lot.

    Sunday, June 18, 2017 12:10 PM

Answers

  • User-271186128 posted

    Hi hkbeer,

    What is the Connectionstring I can use in web.config ?

    You could refer to the following code to set the Azure Storage connection string:

    Replace account-name with the name of your storage account, and account-key with your account access key:

    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
        </startup>
        <appSettings>
            <add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key" />
        </appSettings>
    </configuration>

    How to create this table and insert the 2 records above ?

    Then, you could refer to the following code to create table.

    // Retrieve the storage account from the connection string.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));
    
    // Create the table client.
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    
    // Retrieve a reference to the table.
    CloudTable table = tableClient.GetTableReference("people");
    
    // Create the table if it doesn't exist.
    table.CreateIfNotExists();

    and using the following code to insert entity to Table:

    // Retrieve the storage account from the connection string.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));
    
    // Create the table client.
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    
    // Create the CloudTable object that represents the "people" table.
    CloudTable table = tableClient.GetTableReference("people");
    
    // Create a new customer entity.
    CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
    customer1.Email = "Walter@contoso.com";
    customer1.PhoneNumber = "425-555-0101";
    
    // Create the TableOperation object that inserts the customer entity.
    TableOperation insertOperation = TableOperation.Insert(customer1);
    
    // Execute the insert operation.
    table.Execute(insertOperation);

    After that, how can I get this 2 rows of data into my sqldatasource ?

    Do you mean you want to use SQLDataSource control with Azure Storage Table? Please remember that the SQLDataSource control is used to access data that is located in a relational database, but Table Storage stores NoSQL data.

    If you want to combine the data source, I suggest you could use the Concat method or Union method.

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 22, 2017 5:50 AM

All replies

  • User1289604957 posted

    Hello,

    Here you go : https://docs.microsoft.com/en-us/azure/storage/storage-dotnet-how-to-use-tables

    Best regards,

    Maher

    Sunday, June 18, 2017 1:16 PM
  • User-271186128 posted

    Hi hkbeer,

    What is the Connectionstring I can use in web.config ?

    You could refer to the following code to set the Azure Storage connection string:

    Replace account-name with the name of your storage account, and account-key with your account access key:

    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
        </startup>
        <appSettings>
            <add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key" />
        </appSettings>
    </configuration>

    How to create this table and insert the 2 records above ?

    Then, you could refer to the following code to create table.

    // Retrieve the storage account from the connection string.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));
    
    // Create the table client.
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    
    // Retrieve a reference to the table.
    CloudTable table = tableClient.GetTableReference("people");
    
    // Create the table if it doesn't exist.
    table.CreateIfNotExists();

    and using the following code to insert entity to Table:

    // Retrieve the storage account from the connection string.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));
    
    // Create the table client.
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    
    // Create the CloudTable object that represents the "people" table.
    CloudTable table = tableClient.GetTableReference("people");
    
    // Create a new customer entity.
    CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
    customer1.Email = "Walter@contoso.com";
    customer1.PhoneNumber = "425-555-0101";
    
    // Create the TableOperation object that inserts the customer entity.
    TableOperation insertOperation = TableOperation.Insert(customer1);
    
    // Execute the insert operation.
    table.Execute(insertOperation);

    After that, how can I get this 2 rows of data into my sqldatasource ?

    Do you mean you want to use SQLDataSource control with Azure Storage Table? Please remember that the SQLDataSource control is used to access data that is located in a relational database, but Table Storage stores NoSQL data.

    If you want to combine the data source, I suggest you could use the Concat method or Union method.

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 22, 2017 5:50 AM