我正在使用 Visual Studio Code,并且正在尝试连接到我的 MySql 服务器 (5.5).我已经下载了最新的 (6.10.6) MySql 连接器并安装了它.我不得不研究在 VS Code 中添加引用,但我做到了.现在我的 .csproj 文件如下所示:
I am using Visual Studio Code, and I'm trying to connect to my MySql server (5.5). I have downloaded the latest (6.10.6) MySql connector and installed it. I had to research to add references in VS Code, but I managed it. Now my .csproj file looks like this:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp2.0</TargetFramework> </PropertyGroup> <ItemGroup> <Reference Include="Lib\MySql.Data.dll"> <HintPath>MySql.Data</HintPath> <SpecificVersion>False</SpecificVersion> </Reference> </ItemGroup> </Project>我已将 C:\Program Files (x86)\MySQL\MySQL Connector Net 6.10.6\Assemblies\v4.5.2 的内容复制到 myProject\Lib.在编译时没有错误,都是绿色的.但是当我尝试使用此代码进行连接时
I have copied the content of C:\Program Files (x86)\MySQL\MySQL Connector Net 6.10.6\Assemblies\v4.5.2 to myProject\Lib. At compile time there are no errors, all green. But when I try to connect using this code
using System.Data; using System.Security.Permissions; using MySql.Data; using MySql.Data.MySqlClient; public static bool Connect(){ string str = "server=192.168.0.160;user=usr;database=DB;port=3306;password=pass"; MySqlConnection connection = new MySqlConnection(str); connection.Open(); System.Console.WriteLine(connection.State); return true; }它失败了,并产生了这个错误:
it fails, and produces this error:
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in MySql.Data.dll: 'Could not load file or assembly 'System.Security.Permissions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system can't find the file.'我不知道为什么,因为 using 指令没有发出错误信号.
I do not know why, as the using directive is not signaling an error.
推荐答案程序集 System.Security.Permissions 当前不适用于 .NET 核心应用程序,所以我猜您使用的是旧版本与 .NET core 2 不兼容的 MySQL Database Provider.
The assembly System.Security.Permissions is currently not available for .NET core applications so my guess is you are using an older version of MySQL Database Provider that is not compatible with .NET core 2.
根据官方文档.NET Core 2.0 仅从 6.10 版本开始支持.
According the official documentation .NET core 2.0 is only supported from version 6.10.
尝试从以下位置安装最新版本:dev.mysql/downloads/connector/net/6.10.html
Try installing the latest version from: dev.mysql/downloads/connector/net/6.10.html
编辑
如果您已经拥有该版本但它仍然无法运行,则可能是您缺少一些参考资料.为什么不尝试使用官方的 NuGet 而不是引用 GAC 中的 dll,命令如下:
If you already have that version and it is still not working, might be that you are missing some references. Why don't you try using the official NuGet instead of referencing the dll in the GAC, here is the command:
Install-Package MySql.Data -Version 6.10.6如果您使用 VS Code,则可以使用 NuGet 包管理器扩展直接从编辑器管理包:marketplace.visualstudio/items?itemName=jmrog.vscode-nuget-package-manager
If you are using VS Code, you can use the NuGet package manager extension to manage the packages directly from the editor: marketplace.visualstudio/items?itemName=jmrog.vscode-nuget-package-manager
编辑 2
似乎这可能是一个错误,因为我发现了这个问题 .NET Core 2 with MySql.Data 导致权限错误 接受的答案建议更新到版本 8.
Seems it might be a bug as I found this question .NET Core 2 with MySql.Data results in permission error and the accepted answer recommends updating to version 8.
所以尝试更新到版本 8.0.10-rc 并让问题消失,这是 NuGet 命令:
So try to update to version 8.0.10-rc and let the problem be gone, here is the NuGet command:
Install-Package MySql.Data -Version 8.0.10-rc更多推荐
MySql.Data.dll 中的 IO.FileNotFoundException:无法加载 System.Security.Permissions
发布评论