Xamarin 与 SQL Server 的通信 [英] Xamarin communication with SQL Server

查看:124
本文介绍了Xamarin 与 SQL Server 的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个项目,其中必须有 3 个必须连接到数据库的平台(ASP、桌面(Windows 窗体)和 Xamarin).我一直在试验,但似乎无法直接连接到数据库.Sqlserver 只是在几秒钟后关闭连接.所以我尝试了webservice的方式.我无法添加服务引用.

I'm making a project where I must have 3 platforms (ASP, Desktop (windows forms) and Xamarin) that must connect to a database. I've been experimenting but it seems I can't connect to the database directly. Sqlserver just closes the connection a few seconds later. So I tried the webservice way. I couldn't add a service reference.

我进行了搜索,看来我必须:在 PCL 中卸载 Xamarin.Forms nugget 包,在解决方案属性中取消选中 Windows Phone,重新启动 VS 并重新安装 nugget 包.然后我就可以添加服务引用了.

I searched and it seems I had to: uninstall Xamarin.Forms nugget package in PCL, uncheck the Windows Phone in the solution properties, restart VS and reinstall the nugget package. And then I was able to add the service reference.

但后来它说我需要 Sytem.Web.Services 引用才能工作.但是我不能添加它...

But then it said I needed the Sytem.Web.Services reference for it to work. But I can't add it...

那么,在这一切之后,我怎么可能像这样连接到 SQL Server?在 ASP 和桌面中它工作正常,我通过包含所有连接的类库直接连接.但我正在为这个问题挠头……

So, after all this, how can I possibly connect to SQL Server like this? In ASP and Desktop it works fine, I connect directly, via a class library which contains all the connections. But I'm scratching my head about this one...

有什么想法吗?

推荐答案

当您有多个应用程序需要访问相同的数据时,最好在它们之间放置一些可以相互通信的东西.这减少了跨应用程序的重复逻辑数量,意味着您只需要将数据库公开给单个服务.

When you have multiple applications that needs to access the same data, you're better of putting something between them that they all communicate with. This reduces the amount of duplicated logic across applications and means you only need to expose your database to a single service.

                        Database
                           |
                           |
                           |
                          API
                          /|\
                         / | \
                        /  |  \
                       /   |   \
                      /    |    \
            Mobile---   Desktop  ---Website

如果您对所有内容都使用 .NET 堆栈,则您的 API 可能会在 中实现ASP.NET Web API 或 WCF.我推荐 Web API,因为它更简单.任何能说 HTTP 的东西都可以与之对话.

If you're using the .NET stack for everything, your API is probably going to be implemented in ASP.NET Web API or WCF. I recommend Web API, because it's simpler. Anything that can speak HTTP can talk to it.

然后,为了与您的 API 进行通信,您通常会围绕低级客户端创建一个包装器,以便与您的 API 端点进行简单的通信.这通常采用围绕 HttpClient 或 RestSharp 之类的类库的形式.该类库通过任何 .NET 应用程序都可以使用的 NuGet 包提供.

Then, to communicate with your API, you generally create a wrapper around an low level client that makes it simple to communicate with your API's endpoints. This often takes the form of a class library wrapped around something like HttpClient or RestSharp. This class library is delivered via NuGet package that any .NET application can consume.

这是一些伪代码(不要指望它会编译)

Here's some pseudo code (don't expect it to compile)

namespace MyClientAPI
{
    public class MyClient
    {
        private readonly string _baseAddress;

        public MyClient(string baseAddress)
        {
            _baseAddress = baseAddress;
        }

        public List<Customer> GetCustomers()
        {
            var restClient= new RestClient(_baseAddress);
            var request = new RestRequest("customers/all");
            var customers = restClient.Execute<List<Customer>>(request);
            return Customers;
        }
    }
}

这篇关于Xamarin 与 SQL Server 的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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