uwp连接mysql数据库_如何从Windows 10 UWP应用连接到SQL Server数据库

小编典典

通过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;

}

如果您需要支持比Fall Creators Update更低的版本,则还可以通过Desktop Bridge从UWP应用程序包中调用SqlClient

API。我为此发布了一个示例:https:

//github.com/Microsoft/DesktopBridgeToUWP-

Samples/tree/master/Samples/SQLServer

2020-05-19

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值