简体   繁体   English

如何在 Xamarin 中从 Android 调用 SQL Server 存储过程

[英]How to call SQL Server stored procedure from Android in Xamarin

We have a mobile application made in VB.NET for Windows CE/Mobile smart devices for shipping/reception operations in a warehouse.我们有一个用 VB.NET 制作的移动应用程序,适用于 Windows CE/Mobile 智能设备,用于仓库中的发货/接收操作。 This application connects to a SQL Server and extensively uses stored procedures.此应用程序连接到 SQL Server 并广泛使用存储过程。 Since Windows Mobile devices are discontinued and replaced by Android devices, we have to convert our solution to Android, using Visual Studio's Xamarin and C#.由于 Windows Mobile 设备已停产并被 Android 设备取代,我们必须使用 Visual Studio 的 Xamarin 和 C# 将我们的解决方案转换为 Android。

I'm a newbie at Android programming.我是 Android 编程的新手。 Is there a way we can connect directly to a SQL Server instance and call a stored procedure from Android?有没有办法可以直接连接到 SQL Server 实例并从 Android 调用存储过程? I made some searches and people says it's better to call web services as an intermediary between Android and SQL Server.我进行了一些搜索,人们说最好将 Web 服务称为 Android 和 SQL Server 之间的中介。 Is it the best practice?这是最佳做法吗?

Thanks for your insight and help感谢您的洞察力和帮助

Upgrading Legacy applications can be incredibly complicated depending on the technology and frameworks used.根据所使用的技术和框架,升级旧版应用程序可能非常复杂。

The first thing I would suggest taking a look at is the Architecture documentation Microsoft produced for Xamarin and Cross-platform frameworks which you can see here我建议看的第一件事是 Microsoft 为 Xamarin 和跨平台框​​架制作的架构文档,您可以在此处查看

Typical Application Layers典型应用层

  • Data Layer – Non-volatile data persistence, likely to be an SQLite database but could be implemented with XML files or any other suitable mechanism.数据层——非易失性数据持久性,可能是一个 SQLite 数据库,但可以用 XML 文件或任何其他合适的机制来实现。

  • Data Access Layer – Wrapper around the Data Layer that provides Create, Read, Update, Delete (CRUD) access to the data without exposing implementation details to the caller.数据访问层——数据层的包装,提供对数据的创建、读取、更新、删除 (CRUD) 访问,而不会将实现细节暴露给调用者。 For example, the DAL may contain SQL statements to query or update the data but the referencing code would not need to know this.例如,DAL 可能包含查询或更新数据的 SQL 语句,但引用代码不需要知道这一点。

  • Business Layer – (sometimes called the Business Logic Layer or BLL) contains business entity definitions (the Model) and business logic.业务层——(有时称为业务逻辑层或 BLL)包含业务实体定义(模型)和业务逻辑。 Candidate for Business Façade pattern. Business Facade 模式的候选者。

  • Service Access Layer – Used to access services in the cloud: from complex web services (REST, JSON, WCF) to simple retrieval of data and images from remote servers.服务访问层——用于访问云中的服务:从复杂的 Web 服务(REST、JSON、WCF)到从远程服务器简单检索数据和图像。 Encapsulates the networking behavior and provides a simple API to be consumed by the Application and UI layers.封装网络行为并提供一个简单的 API 供应用程序和 UI 层使用。

  • Application Layer – Code that's typically platform specific (not generally shared across platforms) or code that is specific to the application (not generally reusable).应用程序层 – 通常特定于平台的代码(通常不跨平台共享)或特定于应用程序的代码(通常不可重用)。 A good test of whether to place code in the Application Layer versus the UI Layer is (a) to determine whether the class has any actual display controls or (b) whether it could be shared between multiple screens or devices (eg. iPhone and iPad).将代码放置在应用程序层还是 UI 层的一个很好的测试是 (a) 确定该类是否具有任何实际的显示控件或 (b) 是否可以在多个屏幕或设备(例如 iPhone 和 iPad)之间共享)。

  • User Interface (UI) Layer – The user-facing layer, contains screens, widgets and the controllers that manage them.用户界面 (UI) 层——面向用户的层,包含屏幕、小部件和管理它们的控制器。

Now you could just simply use the System.Data.SqlClient assembly and fire off stored procedure runs against your database.现在您可以简单地使用System.Data.SqlClient程序集并触发针对您的数据库运行的存储过程。 However a more common approach would to create an REST Api that sits in-between your client and your back-end services.然而,更常见的方法是创建一个位于客户端和后端服务之间的 REST Api。

You can download a handy E-book created by the Devs over at microsoft that will show you some common enterprise patterns to use for Cross-platform technologies like Xamarin which can be found here您可以下载由开发人员在 microsoft 上创建的方便的电子书,该书将向您展示一些用于跨平台技术(如 Xamarin)的常见企业模式,可在此处找到

Here's one such example of the kind of patterns those links refer to.这是这些链接所指的模式类型的一个示例。 手机api sql架构示例

You can also find an overview of various web services that you can use at this link您还可以在此链接中找到您可以使用的各种网络服务的概述

The options it gives you an overview of are:它为您提供的选项概述是:

  • ASMX ASMX
  • WCF周转基金
  • REST休息

So plenty of choice, but depends on your current approach.有很多选择,但取决于您当前的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM