在上一篇中,我们通过 SSDS Explorer 详细了解了 SDS 的工作和操作流程。这一篇,我们会详细讲解如何使用程序员的方法来操作 SDS。
SDS 提供 SOAP 和 REST 两种接口,这里我们是用 REST + C# 的方法来讲解。SOAP 与之殊途同归,请有兴趣的同学自己查阅 MSDN。
闲话少说,下面我们以创建 Authority 为例,给出 REST 操作 SDS 的“万能框架”:
public string CreateAuthority()
{
//步骤一:通过某种方式,构造要发送到服务的 XML 数据。
//显然,这一部分只有在 POST 方法和 PUT 方法时才会有,
//对于 GET 方法和 DELETE 方法则不需要。
//下面示例的是一个创建 Authority 的 XML。
const string AuthorityTemplate = @"<s:Authority xmlns:s='http://schemas.microsoft.com/sitka/2008/03/'>
<s:Id>{0}</s:Id>
</s:Authority>";
if (String.IsNullOrEmpty(ServiceUri))
{
throw new ArgumentOutOfRangeException("ServiceUri");
}
string authorityUri = null;
try
{
string requestPayload = string.Format(AuthorityTemplate, authorityId);
//步骤二:建立用以传送数据的 HttpWebRequest 对象。
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ServiceUri);
//步骤三:将用户名和密码放置在 HttpWebRequest 对象的 Credentials 中。
request.Credentials = new NetworkCredential(userName, password);
//步骤四:设置 HTTP 方法。
//HTTP 方法与数据操作方法的对照:POST=create; PUT=update; DELETE=delete; GET=retrieve。
//在这个例子中,我们需要 Create,所以选择 POST 方法。
request.Method = "POST";
//步骤五:传送数据到服务器。
//显然,这一部分只有在 POST 方法和 PUT 方法时才会有,对于 GET 方法和 DELETE 方法则不需要。
UTF8Encoding encoding = new UTF8Encoding();
request.ContentLength = encoding.GetByteCount(requestPayload);
request.ContentType = XmlContentType;
//传送数据。
using (Stream reqStm = request.GetRequestStream())
{
reqStm.Write(encoding.GetBytes(requestPayload), 0, encoding.GetByteCount(requestPayload));
}
//步骤六:获取服务器响应,根据服务器的相应获取操作结果。
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode != HttpStatusCode.Created)
{
Console.WriteLine("Failed to create authority");
}
authorityUri = "https://" + authorityId + ".data.database.windows.net/v1/";
}
catch (WebException ex)
{
Console.Error.WriteLine(ex);
HttpWebResponse response = ex.Response as HttpWebResponse;
if (response != null)
{
string errorMsg = ReadResponse(response);
Console.WriteLine(string.Format("Error: {0}", errorMsg));
Console.WriteLine("Unexpected status code returned: {0} ", response.StatusCode);
}
}
return authorityUri;
}
很简单吧? 所有的操作与这个“万能框架”相似。例如,我们稍微做一些改动,就可以得到下面这样一个删除 Entity 的函数:
public string DeleteEntity(string entityId)
{
//由于删除是 HTTP 中的 Delete 操作,所以无步骤一和步骤五。
string EntityUri = string.Format("https://{0}.data.database.windows.net/v1/{1}/{2}",
authorityId, containerId, entityId);
try
{
WebRequest request = HttpWebRequest.Create(EntityUri); //步骤二
request.Credentials = new NetworkCredential(userName, password); //步骤三
request.Method = "DELETE"; //步骤四
//步骤六
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode != HttpStatusCode.OK)
{
Console.WriteLine("Failed to delete the entity resource.");
}
}
}
catch (WebException ex)
{
string errorMsg = ex.Message;
Console.WriteLine("Deletion failed: {0}", errorMsg);
if (ex.Response != null)
{
using (HttpWebResponse response = ex.Response as HttpWebResponse)
{
Console.WriteLine("Unexpected status code returned: {0}", response.StatusCode);
}
}
}
}
UPDATE 的操作和“万能框架”中 Create 的方法基本雷同,唯一区别是在步骤四的地方,将 "POST" 改为 "PUT"。
读取数据就不用说了吧?呵呵。。那太容易了,就连把 URI 直接输入浏览器都可以。作为编程来说,其实就是在上面给出的 DeleteEntity() 函数的代码中,步骤四的 HTTP 方法由 DELETE 改为 GET,再大到 HttpWebResponse 中去获取你想要获取的内容。
至此,数据库的四种操作(CRUD:Create、Read、Update、Delete)我们都已经讲过了。对,就是这么容易。
由于时间关系,我只在附件内写出了 Authority、Container、Entity 三种结构的 Create 操作。相信读者看完这篇文章后,自己改写成其他操作并非难事。下载地址:http://www.itpow.com/c/2009/03/69ZBEWGWUAE4GXA9/P40I6GUHYENMBA92.zip
我正把 SDS 应用到我目前的项目之中。如果大家有兴趣的话,我争取过段时间提取一部分功能,做成 Demo 供大家参考。感谢大家的关注!by 流牛木马