"Azure Services Platform Step by step-4th" SQL Data Services Programming Basics

Source: Internet
Author: User
Keywords HTTP step we
Tags basics check code console create creating data data services
In the previous article, we learned more about SDS's work and operation process through SSDs Explorer.

In this article, we will explain in detail how to use the programmer's method to manipulate SDS.

SDS provides SOAP and rest two kinds of interfaces, here we use rest+c# method to explain. Soap is the same as it is, please check MSDN with interested students.

Less gossip, let's take the example of creating authority, giving the "universal framework" of Rest-operation SDS:

public String createauthority ()


       {


//Step One (blue): In some way, construct the XML data to be sent to the service. Obviously, this part is only available when the Post method and the Put method are used, not for the Get method and the Delete method. The following example is an XML
that creates authority

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);


//Step two: Establish a HttpWebRequest object to transmit data


HttpWebRequest request = (HttpWebRequest) httpwebrequest.create (Serviceuri);


//Step three: Place the username and password in the credentials of the HttpWebRequest object


request. Credentials = new NetworkCredential (userName, password);





//Step Four (: Set HTTP method.) Comparison between HTTP method and data manipulation method: Post=create; Put=update; Delete=delete; Get=retrieve. In this example, we need to create, so we select the Post method.


request. method = "POST";





//Step Five (Blue part): Transfer data to server. Obviously, this part is only available when the Post method and the Put method are used, not for the Get method and the Delete method.





utf8encoding encoding = new UTF8Encoding ();


request. ContentLength = Encoding. GetByteCount (Requestpayload);


Request. ContentType = Xmlcontenttype;





//Transmit Data


using (Stream reqstm = Request. GetRequestStream ())


               {


reqstm.write (encoding. GetBytes (Requestpayload), 0, encoding. GetByteCount (requestpayload));


               }             


//Step six: Get the server response and get the results of the operation according to the server


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;


       }

Is it easy? All operations are similar to this "universal framework". For example, if we make a few changes, we get the following function to delete entity:

Public string deleteentity (string EntityId)
{
//Because deletion is a delete operation in HTTP, there is no step and step five

String Entityuri = String. Format ("Https://{0}.data.database.windows.net/v1/{1}/{2}",
Authorityid, cont Ainerid, EntityId);
Try
{
//Step two: WebRequest request = Httpwebrequest.create (Entityuri);
Step three: Request. Credentials = new NetworkCredential (userName, password);
//Step four: request.             method = "DELETE";
//Step Six: using (httpwebresponse response = (HttpWebResponse) request. GetResponse ())
{
if (response). StatusCode!= Httpstatuscode.ok)
{
Console.WriteLine ("Failed to delete" entity Resour CE. ");
}
}
}
catch (WebException ex)
{
String Erro RMSG = 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 operations and the "universal framework" in the Create method is basically the same, the only difference is in step 4, the "POST" to "put." For example, the following function:

Read the data. Oh ... That's too easy, even to enter the URI directly into the browser. But as a matter of programming, in the Code of the Deleteentity () function given above, the HTTP method of step four is changed from delete to get, and then to HttpWebResponse for what you want to get.

So far, four operations of the database (Crud,create,read,update,delete) We've talked about it. Yes, that's easy.

Due to the time relationship, I only wrote the create operation of the authority,container,entity three structures in the attachment. After reading this article, I believe that it is not difficult for the reader to rewrite itself into other operations.

I'm applying SDS to my current project. If you are interested, I fought for a period of time to extract part of the function, made demo for everyone's reference.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.