本文将会介绍如何使用Table Storage。Table Storage提供给我们一个云端的表格结构。我们可以把他想象为XML文件或者是一个轻量级的数据库(当然,不是通过SQL 语句进行数据的操作)。
为了方便.NET开发人员,我们在SDK中提供了Microsoft.WindowsAzure.StorageClient类来帮助发送REST请求。
步骤一:创建解决方案和项目
由于我们要在本地模拟环境下测试Table Storage,首先,请确保Development Storage的管理器程序已经启动。我们可以找到管理器的进程手动启动或者让Visual Studio帮助我们启动他。
右击工具栏中Development Fabric的图标,选择”Show Development Storage UI”。
我们要关注的是Service management中Table所在的一行。要确保Status为Running。
确认完毕后启动Visual Studio,并且新建一个Console项目。
步骤二:添加程序集引用
在Console项目中添加对C:\Program Files\Windows Azure SDK\v1.1\ref\Microsoft.WindowsAzure.StorageClient.dll的引用。该路径为SDK默认安装路径,如果你不能在这个路径中找到Microsoft.WindowsAzure.StorageClient.dll请从SDK安装路径中寻找。
接下来添加对System.Data.Services.Client程序集的引用。该程序集安装在GAC中。你能够在Add Reference窗口的.NET标签下找到他。
步骤三:添加代码
首先在项目中的Program.cs中引用命名空间:
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
然后在Program.cs中添加如下代码:
步骤四:观察并分析代码
class Program
{
static void Main(string[] args)
{
var storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
var tableStorage = storageAccount.CreateCloudTableClient();
// 检查名为CustomerInfo的表格是否被创建,如果没有,创建它
tableStorage.CreateTableIfNotExist("CustomerInfo");
// 创建表格服务上下文
var context = new CustomerInfoContext(storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials);
// 插入两条客户信息数据,客户ID分别设置为0和1
CustomerInfo ci1 = new CustomerInfo() { CustomerAge = 25, CustomerID = "0", CustomerName = "Mike" };
CustomerInfo ci2 = new CustomerInfo() { CustomerAge = 32, CustomerID = "1", CustomerName = "Peter" };
context.AddObject("CustomerInfo", ci1);
context.AddObject("CustomerInfo", ci2);
context.SaveChanges();
// 查找CustomerID为1的客户数据并显示
Console.WriteLine("Retrieve information of a customer whose ID is 1");
var query = context.CreateQuery("CustomerInfo").Where(c => c.CustomerID == "1").ToList();
var returnedcustomerinfo = query.FirstOrDefault();
Console.WriteLine(string.Format("Customer info retrieved: ID:{0},Name:{1},Age:{2}",
returnedcustomerinfo.CustomerID,returnedcustomerinfo.CustomerName,returnedcustomerinfo.CustomerAge));
// 更新CustomerID为1的客户数据中的年龄
returnedcustomerinfo.CustomerAge = 33;
context.UpdateObject(returnedcustomerinfo);
Console.WriteLine("**Customer Info updated**");
// 重新查询,测试更新效果
Console.WriteLine("Retrieve information of a customer whose ID is 1");
var query2 = context.CreateQuery("CustomerInfo").Where(c => c.CustomerID == "1").ToList();
var returnedcustomerinfo2 = query.FirstOrDefault();
Console.WriteLine(string.Format("Customer info retrieved: ID:{0},Name:{1},Age:{2}",
returnedcustomerinfo2.CustomerID, returnedcustomerinfo2.CustomerName, returnedcustomerinfo2.CustomerAge));
// 删除插入的两条客户数据
context.DeleteObject(ci1);
context.DeleteObject(ci2);
context.SaveChanges();
Console.WriteLine("The records has been deleted");
Console.ReadLine();
}
}
public class CustomerInfo : TableServiceEntity
{
public string CustomerID
{
get { return this.RowKey; }
set { this.RowKey = value; }
}
public string CustomerName { get; set; }
public int CustomerAge { get; set; }
public CustomerInfo()
{
this.PartitionKey = "mypartitionkey";
}
}
public class CustomerInfoContext : TableServiceContext
{
public CustomerInfoContext(string baseAddress, StorageCredentials credentials) :
base(baseAddress, credentials)
{
}
}
步骤三中的代码中,首先我们通过CloudStorageAccount.DevelopmentStorageAccount来说明我们使用的本地的Development Storage自带账户而不是真正的云端存储服务账户。(如果要用真实账户可以使用