如何从Windows 10 UWP应用程序连接到SQL Server数据库

Tob*_*bbe 13 c# sql-server win-universal-app windows-10 uwp

我正在尝试从通用Windows应用程序连接到本地MS SQL数据库.我正在使用UWP制作LOB应用程序,以支持桌面,平板电脑和移动使用.当尝试连接到本地(Intranet)SQL Server数据库时,我习惯使用SqlConnection的实例连接到本地服务器,但由于SqlConnection不包含在UWP中使用的.NET子集中,如何完成什么时候使用UWP?

我查看了官方的Microsoft示例以及操作指南,并且没有发现任何关于不是Azure数据库的数据库连接.DbConnection似乎是一个很好的方法,但不能使用,因为它是抽象的,并且它的子代(例如Data.SqlClient.SqlConnection)似乎不包含在UWP的.NET子集中.

我错过了一些非常明显的东西吗?顺便说一下,有没有人知道UWP的良好命名空间参考?

编辑非重复:建议重复的链接问题适用于Windows 8/8.1应用程序,虽然有一些相似之处,但该问题的已接受答案中的代码将无法在UWP上运行.但原则是相同的,但对于使用UWP构建的Windows 10应用程序应该有更好的技术参考.

Ste*_*SFT 12

借助Windows 10 Fall Creators Update(内部版本16299),UWP应用程序现在可以通过标准的.NET类(System.Data.SqlClient)直接访问SQL Server - 这要归功于UWP中新增的.NET Standard 2.0支持.

这是一个Northwind UWP演示应用程序:https: //github.com/StefanWickDev/IgniteDemos

我们在2017年9月的Microsoft Ignite上演示了这个演示,这里是我们会话的录制内容(对于SQL演示,请跳至23:00):https: //myignite.microsoft.com/sessions/53541

以下是从Northwind数据库中检索产品的代码(请参阅演示中的DataHelper.cs).请注意,它与您为Winforms或WPF应用程序编写的代码完全相同 - 得益于.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;
}
Run Code Online (Sandbox Code Playgroud)

如果您需要支持早于Fall Creators Update的早期版本,还可以通过桌面桥从UWP应用程序包中调用SqlClient API.我在这里发布了一个示例:https: //github.com/Microsoft/DesktopBridgeToUWP-Samples/tree/master/Samples/SQLServer

  • 在答案中添加了相关的代码段.请注意,它与过去15年为Winforms编写的代码完全相同 - 得益于.NET Standard 2.0.没有新的概念需要学习. (2认同)
  • 遗憾的是,16299或更高版本没有手机更新,因此Windows 10移动版无法使用此功能. (2认同)