0

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

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

例外:Unable to connect to any of the specified MySQL hosts.

这是我的代码:

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。

在过去的几个小时里,这一直困扰着我,因此非常感谢您的帮助,谢谢!

4

1 回答 1

1

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

于 2015-03-13T14:45:42.113 回答