我正在尝试将 Windows 上运行的 MySQL Server 8.0 连接到 C#。在此之前,我使用的是 PhpMyAdmin,但现在我已经安装了 MySql 8.0 命令行客户端,我想用它连接我的程序。它正在端口 2208 上运行,因为端口 3306 已被使用。
我尝试在互联网上搜索解决方案,但出现的所有解决方案都是使用 PhpMyAdmin 连接 MySQL。
server = "localhost:2208";
database = "LoginFormApplication";
uid = "root";
password = "";
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
Debug.WriteLine(connectionString);
connection = new MySqlConnection(connectionString);
OpenConnection(); //connecting to database
我正在寻找将 Windows 计算机上运行的 MySQL 服务器连接到 C# 的方法。但即使服务器仍在运行,它也会引发错误。以下是错误:
1. Connection must be open and valid
2. Unable to connect to any of the specified MySQL hosts
3. Exception thrown: 'MySql.Data.MySqlClient.MySqlException' in MySql.Data.dll
4. Exception thrown: 'System.InvalidOperationException' in MySql.Data.dll
打开连接方法:
connection.Open();
最佳答案
但是存在语法错误。我需要在连接字符串中单独声明端口号。我在声明服务器名称之后立即声明端口号
解决方案:
Server=myServerAddress; Port=1234; Database=myDataBase; Uid=myUsername;
Pwd=myPassword;
链接:
Unable to connect to any of the specified mysql hosts. C# MySQL
关于c# - 使用C#连接MySQL Server 8.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57993198/