“MySql.Data.MySqlClient.MySqlConnectAttrs”的类型初始值设定项引发了异常

mkh04yzy  于 2023-03-07  发布在  Mysql
关注(0)|答案(1)|浏览(709)

我正在尝试创建Windows窗体应用程序(VS 2019,VB.NET,MySQL连接器Net 8. 0. 23,MySQL.Data v4. 5. 2,Windows 10)与远程MySQL服务器接口我正在使用正确的服务器名称,用户名,密码和数据库。我可以使用MySQL Workbench在同一系统上使用相同的凭据从这个IP地址连接到数据库。我还可以通过VS 2019中的服务器资源管理器连接到服务器并运行查询。
在尝试使用以下命令进行初始连接时:

conn = New MySqlConnection("server=<server>;user=<username>;password=<password>;database=<database>")
conn.Open()

我收到错误

The type initializer for 'MySql.Data.MySqlClient.MySqlConnectAttrs' threw an exception.

System.TypeInitializationException: The type initializer for 'MySql.Data.MySqlClient.MySqlConnectAttrs' threw an exception.
 ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
File name: 'System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
   at MySql.Data.MySqlClient.MySqlConnectAttrs.InitOSDetails()
   at MySql.Data.MySqlClient.MySqlConnectAttrs..cctor()
   --- End of inner exception stack trace ---
   at MySql.Data.MySqlClient.NativeDriver.SetConnectAttrs()
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.Authenticate(Boolean reset)
   at MySql.Data.MySqlClient.NativeDriver.Authenticate(String authMethod, Boolean reset)
   at MySql.Data.MySqlClient.NativeDriver.Open()
   at MySql.Data.MySqlClient.Driver.Open()
   at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
   at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
   at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
   at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
   at MySql.Data.MySqlClient.MySqlPool.GetConnection()
   at MySql.Data.MySqlClient.MySqlConnection.Open()
   at KDA_POS.clsDatabase.ConnOpen() in [...]\clsDatabase.vb:line 22

显然,最相关的细节是

---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

我到处搜索,但任何建议解决方案的帖子都太旧了--它是针对VS 2017或更早版本的,而且使用的是更老版本的MySQL连接器。似乎很明显,在2020年初/中期,VS和MySQL存在严重的兼容性问题,我看到了一些暗示,从那时起有一些积极的更新,但没有实际的细节。
有没有人找到了在2021年将VS 2019 VB.NET连接到MySQL的方法?我是错过了一个引用还是需要安装System.Management?这样的dll听起来有点太重要了,不能错过...在检查我是否可以添加对System.Management的引用时,它似乎在任何版本中都丢失了。

rekjcdws

rekjcdws1#

我遇到了同样的问题。通过删除mysql.data包并从nuget包中安装mysqlconnector解决了这个问题。在mysql.data之前引用的所有类中添加using语句后,一切都运行良好

相关问题