2015-03-13 72 views
0

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

我看过所有与此相关的其他问题,但无法找到解决方案。当我遍历我的代码时,当与数据库的连接尝试打开时发生异常。

例外: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。

在过去的几个小时里,这已经让我头疼不已,非常感谢!

回答

1

您正在使用SqlServer的数据库,但您使用的是用于与MySQL的交互类,你应该使用SqlConnectionSqlCommand这对于SqlServer的

+0

哦,对我甚至没有意识到有一个区别,我想我需要回到制图板,谢谢! – Celt 2015-03-13 14:46:40

+0

现在找到了分贝,但我有一个新的例外,在这里我们再次去:P感谢您的建议! – Celt 2015-03-13 14:52:54

+0

确保连接字符串是正确的 – 2015-03-13 14:54:32