在上一篇中,我们通过 SSDS Explorer 详细了解了 SDS 的工作和操作流程。这一篇,我们会详细讲解如何使用程序员的方法来操作 SDS。
SDS 提供 SOAP 和 REST 两种接口,这里我们是用 REST + C# 的方法来讲解。
第一篇:Azure Services基础:Azure Services平台
第二篇:Azure Services基础:SQL Data Services(SDS)概述
第四篇:Azure Services基础:.NET Services概述
第五篇:Azure Services基础:理解Windows Azure服务架构
第六篇:Azure Services基础:Windows Azure Storage概览
第七篇:Azure Services基础:ASP.NET Provider概述
闲话少说,下面我们以创建 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;
}
{
//步骤一:通过某种方式,构造要发送到服务的 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;
}