Connecting Windows Phone 8 to Sql server

前端 未结 1 2167
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-15 02:11

As I\'m not that skilled in Windows Phone 8 development I would like to discuss/ask what is the best way to connect my Windows Phone 8 to Sql-Server Database

相关标签:
1条回答
  • 2020-12-15 02:17

    Well, to achieve your goal, I would do:

    1. Build a REST webservice with ASP.NET Web API (http://www.asp.net/web-api) which returns objects (those objects will be translated to json automatically). For example:

      public class MyObject 
      {
        public string Content { get; set; }
      }
      

      Controller for it:

      public class TestController : ApiController
      {
        [HttpGet]
        public MyObject Example() {
          return new MyObject() { Content = "Hello World!" };
        }
      }
      
    2. Use a HTTP client in your win phone project:

      HttpClient client = new HttpClient();
      client.BaseAddress = new Uri("http://mywebservice.com");
      client.DefaultRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
      
      using (var result = await client.GetStreamAsync("test/example"))
      {
        var serializer = new JsonSerializer(); // this is json.net serializer
        using (var streamReader = new StreamReader(result)) {
          using (var jsonReader = new JsonTextReader(streamReader))
          {
            var obj = serializer.Deserialize<MyObject>(jsonReader);
            // you can access obj.Content now to get the content which was created by the webservice
            // in this example there will be "Hello World!"
          }
        }
      }
      

    Sure you can create much more complex objects which will be (de)serialized. Just take a look at the Web API tutorials.

    Within your webservice you can now access any database you want.

    Edit If you need a more detailed answer, leave me a comment.

    0 讨论(0)
提交回复
热议问题