private static void DeleteData() { var connectionString = "your_connection_string"; var tableName = "USERSAMPLE"; var CSAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(connectionString); var CTClient = CSAccount.CreateCloudTableClient(); var CTable = CTClient.GetTableReference(tableName); //確保沒有該張 Table 就建立 var resCreate = CTable.CreateIfNotExistsAsync().Result; System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); Microsoft.WindowsAzure.Storage.Table.TableOperation operation = Microsoft.WindowsAzure.Storage.Table.TableOperation.Delete( new User { RowKey = "USER333", PartitionKey = "GROUP4", ETag = "*" }); var res = CTable.ExecuteAsync(operation).Result; Console.WriteLine("Success Delete"); stopwatch.Stop(); Console.WriteLine(stopwatch.Elapsed.ToString()); }
/// <summary> /// Adds and saves and item to the table /// </summary> internal void InsertOrReplace(ExceptionLog exceptionLog) { // Set some default if (exceptionLog.DateCreated == DateTime.MinValue) { exceptionLog.DateCreated = DateTime.UtcNow; } if (exceptionLog.DateUpdated == DateTime.MinValue) { exceptionLog.DateUpdated = DateTime.UtcNow; } Microsoft.WindowsAzure.Storage.Table.TableOperation insertOrReplace = Microsoft.WindowsAzure.Storage.Table.TableOperation.InsertOrReplace(exceptionLog); // Execute the insert operation. cloudTable.Execute(insertOrReplace); } // InsertOrReplace
private static void ReadOneData() { var connectionString = "your_connection_string"; var tableName = "USERSAMPLE"; var CSAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(connectionString); var CTClient = CSAccount.CreateCloudTableClient(); var CTable = CTClient.GetTableReference(tableName); Microsoft.WindowsAzure.Storage.Table.TableOperation operation = Microsoft.WindowsAzure.Storage.Table.TableOperation.Retrieve <User>("GROUP10", "USER999"); var res = CTable.ExecuteAsync(operation).Result.Result; if (res != null) { Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(res)); } else { Console.WriteLine("NODATA!"); } }
private static void UpdateDataWithEtag() { var connectionString = "your_connection_string"; var tableName = "USERSAMPLE"; var CSAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(connectionString); var CTClient = CSAccount.CreateCloudTableClient(); var CTable = CTClient.GetTableReference(tableName); //確保沒有該張 Table 就建立 var resCreate = CTable.CreateIfNotExistsAsync().Result; System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); Microsoft.WindowsAzure.Storage.Table.TableOperation operation = Microsoft.WindowsAzure.Storage.Table.TableOperation.Retrieve <User>("GROUP10", "USER999"); var user = CTable.ExecuteAsync(operation).Result.Result as User; if (user != null) { Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(user)); user.Name = "使用者" + 999 + "--更改過了第二次!!!"; var operationUpdate = Microsoft.WindowsAzure.Storage.Table.TableOperation.Replace(user); var res = CTable.ExecuteAsync(operationUpdate).Result; Console.WriteLine("Success"); } else { Console.WriteLine("NODATA!"); } stopwatch.Stop(); Console.WriteLine(stopwatch.Elapsed.ToString()); }
public static Microsoft.WindowsAzure.Storage.Table.TableResult GetRandomTableResult() { //try //{ Microsoft.WindowsAzure.Storage.Table.TableResult retreiveResult = null; XmlDocument document = new XmlDocument(); document.Load(@"C:\Users\Administrator\source\repos\ConsoleApp5\ConsoleApp5\bin\Debug\Test.xml"); var appSettings = System.Configuration.ConfigurationManager.AppSettings; if (appSettings.Count == 0) { Console.WriteLine("The appsettings is empty\nEnter a valid ConnectionString in AppConfig file"); Console.WriteLine("Press a key to exit.."); Console.ReadLine(); } else { foreach (var key in appSettings.AllKeys) { Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(System.Configuration.ConfigurationManager.AppSettings[key]); Microsoft.WindowsAzure.Storage.Table.CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); Microsoft.WindowsAzure.Storage.Table.CloudTable table = tableClient.GetTableReference("CustomerInformation"); //table.CreateIfNotExists(); /*foreach (Json_File file in json_Files) * { * Microsoft.WindowsAzure.Storage.Table.TableOperation insertOperation = Microsoft.WindowsAzure.Storage.Table.TableOperation.Insert(file); * table.Execute(insertOperation); * } */ if (table.Exists()) { Console.WriteLine("The table exists.."); //Microsoft.WindowsAzure.Storage.Table.EntityProperty.CreateEntityPropertyFromObject Dictionary <int, string> Partition_Keys = new Dictionary <int, string>(); int num = 1; Microsoft.WindowsAzure.Storage.Table.TableContinuationToken token = null; var entities = new List <Json_File>(); do { var queryResult = table.ExecuteQuerySegmented(new Microsoft.WindowsAzure.Storage.Table.TableQuery <Json_File>(), token); entities.AddRange(queryResult.Results); token = queryResult.ContinuationToken; } while (token != null); foreach (var file in entities) { Partition_Keys.Add(num, file.PartitionKey); num++; } int size = Partition_Keys.Count; Random random = new Random(); int random_number = random.Next(1, size + 1); string RandomPartitionKey = Partition_Keys[random_number]; Microsoft.WindowsAzure.Storage.Table.TableContinuationToken continuationToken = null; var Entities = new List <Json_File>(); do { var rangeQueryResult = table .ExecuteQuerySegmented(new Microsoft.WindowsAzure.Storage.Table.TableQuery <Json_File>() .Where(Microsoft.WindowsAzure.Storage.Table.TableQuery .GenerateFilterCondition("PartitionKey", Microsoft.WindowsAzure.Storage.Table.QueryComparisons.Equal, RandomPartitionKey)), continuationToken); Entities.AddRange(rangeQueryResult.Results); continuationToken = rangeQueryResult.ContinuationToken; } while (continuationToken != null); Dictionary <int, string> Row_Keys = new Dictionary <int, string>(); int number = 1; foreach (var file in Entities) { Row_Keys.Add(number, file.RowKey); number++; } int sub_size = Row_Keys.Count; Random rand = new Random(); int rand_number = rand.Next(1, sub_size + 1); string RandomRowKey = Row_Keys[rand_number]; Microsoft.WindowsAzure.Storage.Table.TableOperation retreiveOperation = Microsoft.WindowsAzure.Storage.Table.TableOperation.Retrieve <Json_File>(RandomPartitionKey, RandomRowKey); retreiveResult = table.Execute(retreiveOperation); } else { Console.WriteLine("Table doesn't exists\nPress a key to exit...."); Console.ReadLine(); } } } return(retreiveResult); /*catch (Exception ex) * { * Console.WriteLine(ex.Message); * Console.WriteLine(ex.StackTrace); * Console.WriteLine(ex.InnerException); * Console.WriteLine(ex.Source); * Console.WriteLine("Press a key to exit..."); * Console.ReadLine(); * } */ }