Reputation: 51
I am looking for a simple C# program to insert data into Azure blob table storage. Could anyone help ?
Please let me know what is wrong in the following code ?.(The code does not throw any error, but simply does not create any table / insert data )
using System;
using System.Threading.Tasks;
using Azure.Data.Tables;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using TableEntity = Microsoft.WindowsAzure.Storage.Table.TableEntity;
using TableClientConfiguration = Microsoft.Azure.Cosmos.Table.TableClientConfiguration;
public class CustomerEntity : TableEntity
{
public CustomerEntity(string lastName, string firstName)
{
this.PartitionKey = lastName;
this.RowKey = firstName;
}
public CustomerEntity() { } // the parameter-less constructor must be provided
public string Email { get; set; }
public string PhoneNumber { get; set; }
}
class Program
{
static void Main(string[] args) {
var tableName = "TestTempTable";
var storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=**********;AccountKey=*******/****==;EndpointSuffix=core.windows.net";
try
{
Console.WriteLine("START");
var storageAccount = CloudStorageAccount.Parse(storageConnectionString);
var tableClient = storageAccount.CreateCloudTableClient();
var table = tableClient.GetTableReference(tableName);
table.CreateIfNotExistsAsync();
Console.WriteLine($"CloudTable name is : {tableClient}");
// Create a new customer entity.
CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
customer1.Email = "[email protected]";
customer1.PhoneNumber = "1234568";
table.ExecuteAsync(TableOperation.Insert(customer1));
Console.WriteLine("Records Inserted");
Console.WriteLine("END");
}
catch (Exception e)
{
Console.WriteLine("Encountered Exception - "+e);
}
}
}
Thanks, Paul.
Upvotes: 2
Views: 7162
Reputation: 2742
The reason your program doesn't throw any error and doesn't insert the entity is that you are not awaiting the asynchronous operation and your program terminates before it succeeds. And that's why the lines such as "Records Inserted" runs like a normal program execution. Consider using await
in your program in these two lines:
await table.CreateIfNotExistsAsync();
await table.ExecuteAsync(TableOperation.Insert(customer1));
Upvotes: 4