gpt4 book ai didi

c# - SQL连接字符串错误: Unable to connect to any of the specified MySQL hosts

转载 作者:行者123 更新时间:2023-11-29 12:20:51 26 4
gpt4 key购买 nike

过去 4 小时我一直试图让我的程序连接到我的数据库,但我无法让它工作。我正在编写一个 WPF 桌面应用程序,它需要连接到数据库以保存用户创建的内容。我已创建数据库并可以在 SQL Management Studio 中查看它,但我无法使连接字符串正常工作。

我已经查看了与此相关的所有其他问题,但找不到解决方案。当我单步执行代码时,尝试打开数据库连接时会发生异常。

异常:无法连接到任何指定的 MySQL 主机。

这是我的代码:

string connectionString = "Data Source=./SQLExpress;Initial Catalog=PrinterEmulator;Integrated Security=True;";

MySqlConnection conn;

try
{
using (conn = new MySqlConnection())
{
conn.ConnectionString = connectionString;
conn.Open(); //Exception occurs here

MySqlCommand cmd = new MySqlCommand("INSERT INTO [PrinterEmulator].[dbo].[LEDM](NamesOfTablesHere) VALUES(ValuesToBeInsertedHere)", conn);

cmd.ExecuteNonQuery();
}
}
catch (MySqlException ex)
{
MessageBox.Show(ex.ToString());
}

我还使用 SQLExpress。

在过去的几个小时里,这让我很头疼,所以非常感谢您的帮助,谢谢!

最佳答案

您正在使用 SqlServer 作为数据库,但您正在使用用于与 MySQL 交互的类,您应该使用 SqlConnection 并且SqlCommand 用于 SqlServer

关于c# - SQL连接字符串错误: Unable to connect to any of the specified MySQL hosts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29035167/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com