如何在 Windows 10 UWP 应用中使用 mysql [英] How to use mysql with Windows 10 UWP app

查看:27
本文介绍了如何在 Windows 10 UWP 应用中使用 mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 mysql 与 Windows 10 UWP 应用程序一起使用.我到处都被告知要下载连接器/NET 并引用 Assemblies/rt MySql.Data.RT.dll.但是在下载了每个连接器 mysql 的网站后,我找不到这个 dll 来引用.我还尝试了所有其他已安装的 dll,每个 dll 都说无法添加对 '' 的引用.该项目针对 '.NETCore',而文件引用针对 '.NETFramework'.这不是支持的方案" 如何将 mysql 与我的新应用程序一起使用?这不再可能了吗?

I am trying to use mysql with a windows 10 UWP app. I am told everywhere to download connector/NET and to reference the Assemblies/rt MySql.Data.RT.dll. however after downloading every connector mysql's website has available I am unable to find this dll to reference. I have also tried every other dll that was installed and every one of them says "a reference to '' could not be added. The project targets '.NETCore' while the file reference targets '.NETFramework'. this is not a supported scenario" How can I use mysql with my new app? is this no longer possible?

推荐答案

你很幸运!如果您的应用面向 Windows 10 Fall Creators Update(版本 16299),您可以连接到外部数据库.

You are in luck! If your app targets the Windows 10 Fall Creators Update (version 16299), you can connect to external databases.

您必须解决的第一个问题是您无法直接从 UWP 应用程序引用 .NET Framework 库.这可以通过创建 .NET Standard 库并从那里引用 .NET Framework 库来规避.

First problem you have to tackle is the fact that you cannot directly reference .NET Framework libraries from UWP apps. This can be circumvented by creating a .NET Standard library and referencing the .NET Framework libarary from there.

不幸的是,这还不能与官方 MySQL 连接器一起使用(由于一些不受支持的引用),但有替代方法.最有前途的是那些支持 .NET Standard 的.例如,MySqlConnector 项目 提供了与官方连接器非常相似的界面.它仍处于测试阶段,但会定期更新和积极开发.

Unfortunately this doesn't work with the official MySQL Connector yet (due to some unsupported references), but there are alternatives. The ones that are the most promising are those that support .NET Standard. For example the MySqlConnector project offers a very similar interface to the official connector. It is still in beta, but is regularly updated and actively developed.

您可以使用 NuGet 非常轻松地将其安装到您的项目中.打开 NuGet 包控制台(工具 -> NuGet 包管理器 -> 包管理器控制台)并输入以下内容:

You can install it into your project very easily using NuGet. Open the NuGet package console (Tools -> NuGet Package Manager -> Package Manager Console) and enter the following:

Install-Package MySqlConnector -Version 0.34.0 

现在您可以照常使用MySqlConnectionMySqlCommand等.

Now you can use MySqlConnection, MySqlCommand, etc. as usual.

string connStr = "server=localhost;user=root;database=uwpconnect;port=3306;password=";
StringBuilder sb = new StringBuilder();
MySqlConnection conn = new MySqlConnection(connStr);
conn.Open();
string sql = "SELECT Name FROM world";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader();

while (rdr.Read())
{
   sb.AppendLine(rdr[0].ToString());
}
rdr.Close();
conn.Close();

这篇关于如何在 Windows 10 UWP 应用中使用 mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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