How to connect to SQL server database from a Windows 10 UWP app(如何从 Windows 10 UWP 应用程序连接到 SQL Server 数据库)
问题描述
我正在尝试从通用 Windows 应用程序连接到本地 MS SQL 数据库.我正在使用 UWP 制作一个 LOB 应用程序,以支持台式机、平板电脑和移动设备的使用.在尝试连接到本地(内联网)SQL 服务器数据库时,我习惯于使用 SqlConnection 连接本地服务器,但是由于UWP中使用的.NET子集中不包含SqlConnection,所以在使用的时候是怎么做到的UWP?
I'm trying to connect to an on-prem MS SQL database from a universal windows app. I'm making a LOB app using UWP, to support desktop, tablet and mobile use. When trying to connect to a local (intranet) SQL server database, I'm used to using an instance of SqlConnection to connect to a local server, but since SqlConnection is not included in the .NET subset used in UWP, how is this done when using UWP?
我查看了 官方 Microsoft 示例 以及 how-to guides,并没有发现任何关于数据库连接的信息Azure 数据库.DbConnection 似乎可以是一个很好的方法,但不能使用,因为它是抽象的,而且是孩子(例如 Data.SqlClient.SqlConnection) 似乎没有包含在 UWP 的 .NET 子集中.
I've looked over the official Microsoft samples as well as the how-to guides, and found nothing there about database connection that is not an Azure database. DbConnection seemed like it could be a good way to go, but can't be used since it's abstract, and it's children (for instance Data.SqlClient.SqlConnection) does not seem to be included in the .NET subset for UWP.
我在这里遗漏了一些非常明显的东西吗?顺便说一句,有人知道 UWP 的良好命名空间参考吗?
Am I missing something super obvious here? As an aside, does anyone know a good namespace reference for UWP?
编辑非重复:建议作为重复的链接问题适用于 Windows 8/8.1 应用程序,虽然有一些相似之处,但该问题的已接受答案中的代码不适用于 UWP.不过原理是一样的,不过应该有更好的技术参考来介绍使用 UWP 构建的 Windows 10 应用.
Edit for non-duplicate: The linked question suggested as a duplicate is for Windows 8/8.1 apps, and while there are some similarities, the code in the accepted answer for that question won't work on UWP. The principle is the same, however, but there should be a better technical reference for Windows 10 apps build with UWP.
推荐答案
借助 Windows 10 Fall Creators Update(内部版本 16299),UWP 应用现在可以通过标准 NET 类 (System.Data.SqlClient) 直接访问 SQL Server - 谢谢到 UWP 中新添加的对 .NET Standard 2.0 的支持.
With the Windows 10 Fall Creators Update (build 16299) UWP apps can now access SQL Server directly via the standard NET classes (System.Data.SqlClient) - thanks to the newly added support for .NET Standard 2.0 in UWP.
这是一个 Northwind UWP 演示应用:https://github.com/StefanWickDev/IgniteDemos
Here is a Northwind UWP demo app: https://github.com/StefanWickDev/IgniteDemos
我们在 2017 年 9 月的 Microsoft Ignite 上展示了这个演示,这是我们的会议记录(SQL 演示跳到 23:00):https://myignite.microsoft.com/sessions/53541
We have presented this demo at Microsoft Ignite in September 2017, here is the recording of our session (skip to 23:00 for the SQL demo): https://myignite.microsoft.com/sessions/53541
这是从 Northwind 数据库中检索产品的代码(参见演示中的 DataHelper.cs).请注意,这与您为 Winforms 或 WPF 应用程序编写的代码完全相同 - 感谢 .NET Standard 2.0:
Here is the code to retrieve the products from the Northwind database (see DataHelper.cs in the demo). Note that it is exactly the same code that you would write for a Winforms or WPF app - thanks to the .NET Standard 2.0:
public static ProductList GetProducts(string connectionString)
{
const string GetProductsQuery = "select ProductID, ProductName, QuantityPerUnit," +
" UnitPrice, UnitsInStock, Products.CategoryID " +
" from Products inner join Categories on Products.CategoryID = Categories.CategoryID " +
" where Discontinued = 0";
var products = new ProductList();
try
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
if (conn.State == System.Data.ConnectionState.Open)
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = GetProductsQuery;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var product = new Product();
product.ProductID = reader.GetInt32(0);
product.ProductName = reader.GetString(1);
product.QuantityPerUnit = reader.GetString(2);
product.UnitPrice = reader.GetDecimal(3);
product.UnitsInStock = reader.GetInt16(4);
product.CategoryId = reader.GetInt32(5);
products.Add(product);
}
}
}
}
}
return products;
}
catch (Exception eSql)
{
Debug.WriteLine("Exception: " + eSql.Message);
}
return null;
}
如果您需要支持比 Fall Creators Update 更早的版本,您还可以通过桌面桥从 UWP 应用程序包调用 SqlClient API.我在这里发布了一个示例:https://github.com/Microsoft/DesktopBridgeToUWP-Samples/tree/master/Samples/SQLServer
If you need to support earlier versions than the Fall Creators Update, there is also a way for you to call SqlClient APIs from your UWP app package, via the Desktop Bridge. I have a sample for this published here: https://github.com/Microsoft/DesktopBridgeToUWP-Samples/tree/master/Samples/SQLServer
这篇关于如何从 Windows 10 UWP 应用程序连接到 SQL Server 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 Windows 10 UWP 应用程序连接到 SQL Server 数据库
基础教程推荐
- c# Math.Sqrt 实现 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01