1. 首先还是按照Azure Storage 的Pkg:
2. 可以下载AzureStorage Explorer 来管理Azure Storage的状态
https://azurestorageexplorer.codeplex.com/
如果是有很多文件要上传到BLOB,建议使用CloudBerry的Azure版本。
测试实体类:
public class Student : TableEntity { public Student() { } public Student(string id, string name, DateTime joinedDate, bool hasGraduated) { Id = id; Name = name; JoinDate = joinedDate; HasGraduated = hasGraduated.ToString(); RowKey = id; PartitionKey = id; } public string Id { get; set; } public string Name { get; set; } public DateTime JoinDate { get; set; } public string HasGraduated { get; set; } }
需要注意的地方就是要继承自TableEntity.
StorageAccount属性:
private static CloudStorageAccount StorageAccount { get { var creds = new StorageCredentials(AccountName, Key); var account = new CloudStorageAccount(creds, useHttps: true); return account; } }
AccountName与Key需要替换为你自己的。
1. 创建表(TableName根据具体情形替换):
public static void CreateTableIfNotExsit() { CloudTableClient tableClient = StorageAccount.CreateCloudTableClient(); // Create the table if it doesn‘t exist. CloudTable table = tableClient.GetTableReference(TableName); table.CreateIfNotExists(); }
2. 插入记录
public static void Insert(Student s) { CloudTableClient tableClient = StorageAccount.CreateCloudTableClient(); // Create the CloudTable object that represents the "student" table. CloudTable table = tableClient.GetTableReference(TableName); // Create the TableOperation that inserts the entity. TableOperation insertOperation = TableOperation.Insert(s); // Execute the insert operation. table.Execute(insertOperation); }
批量插入 - partitionKey需要一致(PartitionKey是优化查询用的,具体可以看这里: https://msdn.microsoft.com/en-us/library/azure/hh508997.aspx)
/// <summary> /// if want to insert in the same patch must have the same partition key /// </summary> /// <param name="students"></param> public static void Insert(IEnumerable<Student> students) { var tableClient = StorageAccount.CreateCloudTableClient(); // Create the CloudTable object that represents the "student" table. var table = tableClient.GetTableReference(TableName); // Create the batch operation. var batchOperation = new TableBatchOperation(); // Add both entities to the batch insert operation. foreach (var s in students) { batchOperation.Insert(s); } // Execute the batch operation. table.ExecuteBatch(batchOperation); }
查询
public static IEnumerable<Student> GetByName(string name) { var tableClient = StorageAccount.CreateCloudTableClient(); var table = tableClient.GetTableReference(TableName); var query = new TableQuery<Student>().Where(TableQuery.GenerateFilterCondition("Name", QueryComparisons.Equal, name)); foreach (Student entity in table.ExecuteQuery(query)) { yield return entity; } }
组合条件查询
public static IEnumerable<Student> GetGraduatedStudentsByName(string name, bool graduated) { var tableClient = StorageAccount.CreateCloudTableClient(); //Create the CloudTable object that represents the "student" table. var table = tableClient.GetTableReference(TableName); // Create the table query. var rangeQuery = new TableQuery<Student>().Where( TableQuery.CombineFilters( TableQuery.GenerateFilterCondition("Name", QueryComparisons.Equal, name), TableOperators.And, TableQuery.GenerateFilterCondition("HasGraduated", QueryComparisons.Equal,graduated ? "True" : "False")) ); // Loop through the results, displaying information about the entity. foreach (Student entity in table.ExecuteQuery(rangeQuery)) { yield return entity; } }
获取单个记录
public static Student Single(string id) { CloudTableClient tableClient = StorageAccount.CreateCloudTableClient(); // Create the CloudTable object that represents the "student" table. CloudTable table = tableClient.GetTableReference(TableName); TableOperation retrieveOperation = TableOperation.Retrieve<Student>(id, id); // Execute the retrieve operation. var ret = table.Execute(retrieveOperation); if (ret == null) { return null; } return (Student)ret.Result; }
修改记录
public static void Update(Student student) { var tableClient = StorageAccount.CreateCloudTableClient(); // Create the CloudTable object that represents the table. var table = tableClient.GetTableReference(TableName); // Create a retrieve operation that takes a entity. var retrieveOperation = TableOperation.Retrieve<Student>(student.Id, student.Id); // Execute the operation. var retrievedResult = table.Execute(retrieveOperation); // Assign the result to a object. var updateEntity = (Student)retrievedResult.Result; if (updateEntity != null) { updateEntity.Name = student.Name; updateEntity.JoinDate = student.JoinDate; updateEntity.HasGraduated = student.HasGraduated; var updateOperation = TableOperation.Replace(updateEntity); // Execute the operation. table.Execute(updateOperation); } }
删除记录
public static void Delete(string id) { var tableClient = StorageAccount.CreateCloudTableClient(); var table = tableClient.GetTableReference(TableName); var retrieveOperation = TableOperation.Retrieve<Student>(id,id); var retrievedResult = table.Execute(retrieveOperation); var deleteEntity = (Student)retrievedResult.Result; // Create the Delete TableOperation. if (deleteEntity != null) { var deleteOperation = TableOperation.Delete(deleteEntity); // Execute the operation. table.Execute(deleteOperation); } }
本文只是为了演示AzureTable的CRUD操作,在具体场景中,不应该使用静态方法,应该是对象。
完整的测试代码:
[TestMethod] public void AzureTable_CRUD_Test() { AzureTableCRUD.CreateTableIfNotExsit(); AzureTableCRUD.Insert(new Student("1", "StuA", DateTime.Parse("2015-3-11 12:00:00 PM"), false)); AzureTableCRUD.Insert(new Student("2", "StuB", DateTime.Parse("2015-3-11 12:00:00 PM"), false)); AzureTableCRUD.Insert(new Student("3", "StuB", DateTime.Parse("2015-3-11 12:00:00 PM"), false)); AzureTableCRUD.Insert(new Student("4", "Gra_Stu_A", DateTime.Parse("2011-3-11 12:00:00 PM"), true)); AzureTableCRUD.Insert(new Student("5", "Gra_Stu_A", DateTime.Parse("2011-3-11 12:00:00 PM"), true)); // - get many var stu = AzureTableCRUD.GetByName("StuB").ToList(); Assert.IsTrue(stu.Count == 2); Assert.IsTrue(stu[0].Name == "StuB"); Assert.IsTrue(stu[1].Name == "StuB"); // - get single var stuA = AzureTableCRUD.Single("1"); Assert.IsTrue(stuA.Name == "StuA"); // - combine condition get many var graduates = AzureTableCRUD.GetGraduatedStudentsByName("Gra_Stu_A", true).ToList(); Assert.IsTrue(graduates.Count == 2); Assert.IsTrue(graduates[0].Id == "4"); Assert.IsTrue(graduates[1].Id == "5"); AzureTableCRUD.Update(new Student("2","StuB_Modified",DateTime.Parse("2015-4-11 12:00:00"),false )); var stuB = AzureTableCRUD.Single("2"); Assert.IsTrue(stuB.Name == "StuB_Modified"); Assert.IsTrue(stuB.JoinDate.Month == 4); AzureTableCRUD.Delete("4"); var tryGetForth = AzureTableCRUD.Single("4"); Assert.IsNull(tryGetForth); }
在Azure Explorer中查看结果:
时间: 2024-10-01 12:41:39