将 Windows Phone 8 连接到 Sql 服务器 [英] Connecting Windows Phone 8 to Sql server

查看:18
本文介绍了将 Windows Phone 8 连接到 Sql 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我在 Windows Phone 8 开发方面不是很熟练,我想讨论/询问什么是将我的 Windows Phone 8 连接到 Sql-Server 数据库的最佳方式.我试图通过互联网搜索,发现只有少数指南对我使用 WebService 不起作用Linq-to-Sql.这是我失败的地方.

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. I tried to search through the internet and found only few guides which didn't worked for me Linq-to-Sql using WebService. This is where I failed.

首先我想显示数据 - 这是最重要的,我还不需要编辑它们.但未来不可避免.

Firstly I would like to display data - this is most important, I don't need to edit them yet. But in future it is inevitable.

如果编辑有效,我需要直接在我连接的 Sql Server 上编辑它们.我还检查了 Sql Server Compact指南,但只能在 CE 4.0 下工作(当将数据从 Sql Server 导出到 Sql Compact)不支持 Windows Phone 8.但即使它可以工作,它也只是简单地将 Sql server 数据库复制到 Sql Compact,并且不能直接处理 Sql Server 上的数据(这是可以理解的,因为它是 Sql Server Compact).

If the editing would work I need to edit them directly on Sql Server to which I'm connected. I also checked Sql Server Compact guide but that can only work under CE 4.0 (When exporting data from Sql Server to Sql Compact) which doesn't support Windows Phone 8. But even if it would work it simply copy the Sql server database to Sql Compact and doesn't work with data directly on Sql Server (which is understandable because it is Sql Server Compact).

因此,当我深入搜索时,唯一的方法是使用 WebService,我遵循了 YouTube 但正如我之前提到的,问题出在 显示数据 作为指南引导我使用 ListBox,因为它适用于 Windows Phone 7.1 而在 Windows Phone 8 中只是 LongListSelector.

So as I was searching deeply the only way to do that is using WebService I followed some step-by-step guides on YouTube but as I mentioned before, the problem was with displaying data as the guide led me to using ListBox because it was for Windows Phone 7.1 and in Windows Phone 8 is only LongListSelector.

我还发现了问题 连接 Windows Phone 和 Windows8 个 SQL Server 应用程序,这对我很有帮助.

I also found question Connect Windows Phone and Windows 8 apps to SQL Server which was quiet helpful for me.

我想我需要一些分步指南.所以想请问大家有没有WP8和Sql Server连接的分步指南?如果有人愿意编辑我的代码这里让它发挥作用.

I think I need some step-by-step guide how-to. So I would like to ask you if there is any step-by-step guide how to connect WP8 and Sql Server? If someone would be that kind and edit my code here to make it work.

感谢您花时间阅读本文和回答/评论.

Thank you for your time reading this and answers/comments.

硕士

推荐答案

好吧,为了实现您的目标,我会这样做:

Well, to achieve your goal, I would do:

  1. 使用 ASP.NET Web API 构建 REST 网络服务 (http://www.asp.net/web-api) 返回对象(这些对象将自动转换为 json).例如:

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

控制器:

public class TestController : ApiController
{
  [HttpGet]
  public MyObject Example() {
    return new MyObject() { Content = "Hello World!" };
  }
}

  • 在您的 win 手机项目中使用 HTTP 客户端:

  • 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!"
        }
      }
    }
    

  • 当然,您可以创建将(反)序列化的更复杂的对象.只需查看 Web API 教程即可.

    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.

    这篇关于将 Windows Phone 8 连接到 Sql 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆