我正在使用 Visual Studio Code,并且正在尝试连接到我的 MySql 服务器 (5.5)。我已经下载了最新的(6.10.6)MySql 连接器并安装了它。我必须研究如何在 VS Code 中添加引用,但我成功了。现在我的 .csproj 文件如下所示:
<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。编译时没有错误,都是绿色的。但是当我尝试使用此代码进行连接时
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;
}
它失败了,并产生以下错误:
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 指令没有发出错误信号。
最佳答案
程序集System.Security.Permissions
目前不适用于.NET core应用程序,因此我猜测您使用的是与.NET core 2不兼容的旧版本MySQL数据库提供程序。
根据official documentation仅从版本 6.10 开始支持 .NET core 2.0。
尝试安装最新版本:https://dev.mysql.com/downloads/connector/net/6.10.html
编辑
如果您已经拥有该版本但仍然无法正常工作,则可能是您缺少一些引用资料。为什么不尝试使用官方的 NuGet 而不是引用 GAC 中的 dll,命令如下:
Install-Package MySql.Data -Version 6.10.6
如果您使用的是 VS Code,则可以使用 NuGet 包管理器扩展直接从编辑器管理包:https://marketplace.visualstudio.com/items?itemName=jmrog.vscode-nuget-package-manager
编辑2
似乎这可能是一个错误,因为我发现了这个问题 .NET Core 2 with MySql.Data results in permission error并且接受的答案建议更新到版本 8。
所以尝试更新到版本8.0.10-rc并让问题消失,这是NuGet命令:
Install-Package MySql.Data -Version 8.0.10-rc
关于c# - MySql.Data.dll 中的 IO.FileNotFoundException : Can't load System. 安全性.权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48602743/