公告
:本博客为微软云计算中文博客
的镜像博客。
部分文章因为博客兼容性问题
,会影响阅读体验
。如遇此情况,请访问
原博客
。
本文是
Windows Azure
入门教学
的第六篇文章。
本文将会介绍如何使用
Table Storage。
Table Storage提供给我们一个云端的表格结构。我们可以把他想象为
XML文件或者是一个轻量级的数据库(当然,不是通过
SQL
语句进行数据的操作)。
使用
Table Storage的方法依然是调用
REST
API。有关
TableStorageRESTAPI的详细信息,请参见
Table服务
API
:
为了方便
.NET开发人员,我们在
SDK中提供了
Microsoft.WindowsAzure.StorageClient类来帮助发送
REST请求。
在开始本教学之前,请确保你从
Windows Azure
平台下载
下载并安装了最新的
Windows Azure开发工具。
本教学使用
Visual Studio 2010作为开发工具。
步骤一:创建解决方案和项目
由于我们要在本地模拟环境下测试
TableStorage,首先,请确保
Storage Emulator已经启动。我们可以找到管理器的进程手动启动或者让
Visual Studio 2010帮助我们启动他。
右击工具栏中
Windows Azure模拟器的图标,选择”
Show Storage Emulator UI”。弹出如下图所示的窗口:
我们要关注的是
Service management中
Table所在的一行。要确保
Status为
Running。
确认完毕后启动
Visual Studio 2010,并且新建一个
Console项目。
步骤二:添加程序集引用
请在项目属性页里确认项目的
Target framework的值是
.NET Framework 4或
.NET Framework
3.5。然后添加对
C:\Program Files\Windows Azure
SDK\v1.3\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"
};
context.AddObject("CustomerInfo"
, ci1);
CustomerInfo
ci2 =
new
CustomerInfo
() { CustomerAge = 32, CustomerID
=
"1"
, CustomerName =
"Peter"
};
context.AddObject("CustomerInfo"
, ci2);
context.SaveChanges();
//
查找
CustomerID
为
1
的客户数据并显示
Console
.WriteLine("Retrieve information of a customer whose ID is
1"
);
var
query =
context.CreateQuery<CustomerInfo
>("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
>("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自带账户而不是真正的云端存储服务账户。
(如果要用真实账户可以使用
//DefaultEndpointsProtocol=https
可以改成
DefaultEndpointsProtocol=http
表示用
HTTP
而不是
HTTPS
CloudStorageAccount
.Parse("DefaultEndpointsProtocol=https;AccountName=[
用户名
];AccountKey=[
密码
]"
);
来实例化对象
)然后通过该账户类来实例化一个
Table客户端类。这两步是使用
SDK中
StorageClient程序集来调用
Table Storage服务的必要步骤。
然后我们需要关注
System.Data.Services.Client程序集。该程序集是
WCF Data Services的客户端程序集,能够帮助我们很方便地创建
OData客户端程序。
(Table
Storage提供的
REST API遵循
OData规范,因此我们的客户端需要遵循
OData规范向
Table
Storage服务发送消息
)
我们需要创建上下文来调用服务,我们可以直接使用
TableServiceContext。但是通常我们会通过写一个继承自
TableServiceContext的类来实现,这样我们可以在该类中添加一些属性或者方法来更加方便地调用。上面的代码中我们继承了
TableServiceContext但是并没有添加额外的代码。在实际应用中我们可以加上一些代码来更加方便地调用。比如我们可以在
CustomerInfoContext
类中加入下面的属性:
public
IQueryable
<CustomerInfo
>
CustomerInfo
{
get
{
return
CreateQuery<CustomerInfo
>("CustomerInfo"
);
}
}
这样我们就可以通过调用
context.CustomerInfo来更加代替
context.CreateQuery<CustomerInfo>("CustomerInfo")。
继承自
TableServiceEntity类的
CustomerInfo
类定义了每条数据的模式。需要注意的是,与一般的关系型数据库不同,
Table Storage并不要求一个表中的所有数据都遵循同一模式。举例来说,在一个表中,可以存储一条有三个字段的记录和一条只有两个字段的记录。这也是我们为什么说可以把
Table
Storage想象为
XML文件的原因。当然在通常情况下我们都会需要在一个表中存储同一模式的数据。这时候我们就可以使用
System.Data.Services.Client程序集来为
Table Storage创建客户端程序。当我们需要在一个表中存储不同模式的数据时我们可以手动构建和发送
REST请求。
还有需要注意的地方是
PartitionKey和
RowKey。这两项共同组合成表的主键。详细信息,请参见
理解
Table服务数据模型
。
代码逻辑包括了对
Table Storage的插入,更新,删除和读取。请参考注释部分。
步骤五:运行程序
如果一切正常,你将会看到
Console程序输出如下信息:
分享到:
相关推荐
Windows Azure使用入门 第二课:建立自己的网站.pdf
Windows Azure入门教学系列
Windows Azure使用入门 第六课:运行开源软件与系统.pdf
Windows Azure使用入门 第一课:创建虚拟机.pdf
Windows Azure使用入门 第五课:用户认证上下打通.pdf
Windows Azure使用入门 第四课:开发测试在云中.pdf
Windows Azure使用入门 第八课:云端备份和灾难恢复.pdf
Windows Azure使用入门 第七课:云端存储和管理数据.pdf
Windows Azure使用入门 第三课:创建云端的数据库.pdf
Windows Azure使用入门 第十课:云端实现媒体点播与直播传送.pdf
Windows Azure使用入门 第九课:开发移动应用和通知发送.pdf
微软云读写共享文件所需依赖
摘要:无责任WindowsAzureSDK.NET开发入门篇,将带来一系列基础文章:从WindowsAzure开发前准备工作、使用AzureAD管理用户信息、创建管理“云”服务、到使用BlobStorage服务等,帮助读者轻易上手使用这套开发工具。...
全书共12章,内容包括云计算概论、云计算技术概观、初探Windows Azure、Windows Azure应用程序开发基础、Windows Azure应用程序开发:Table存储服务、Windows Azure应用程序开发:BLOB存储服务、Windows Azure应用...
Windows Azure 入门系列课程(2):Windows Azure Storage 简介 2008年10月27日,在洛杉矶举行的专业开发者大会PDC2008上,前微软首席软件架构师Ray Ozzie宣布了微软的云计算战略以及云计算平台——Windows Azure。...
Windows Azure入门指南,帮助读者了解Windows Azure使用开发
《Windows Azure从入门到精通》针对微软的云计算平台windows azure,循序渐进地介绍了如何构建和管理云端的可扩展应用,一次一个知识点,同时辅之以适当的练习,可帮助读者轻松掌握基本的编程技能,掌握windows ...
Windows Azure 入门手册1
Windows Azure从入门到精通含源代码,接受微软的云服务平台Windows Azure帮助包含源代码
Microsoft.WindowsAzure.Storage-7.2.1.0