我正在使用 C# 制作我的第一个 Windows 8 应用程序。我有一个我想连接的 MySQL 数据库。我以前用 Windows 窗体做过这个,一切都很顺利。但是,对于 Windows 8 应用程序,它无法连接。
这是我的连接字符串:
string myConnectionString = "Server=mysql9.000webhost.com; Database=a2236339_snooker; Uid=a2236339_joe; password=TeamPr0ject;";
代码如下:
MySqlConnection connection = new MySqlConnection(myConnectionString);
connection.Open();
然后像这样打开我的连接。
我得到的错误是
An exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.RT.DLL but was not handled in user code
谁能解释这是为什么以及我做错了什么?
最佳答案
我看不到您的其余代码,但如果我以正确的方式进行连接,我会这样做:
using (MySqlConnection connection = new MySqlConnection("server=YOUR_SERVER;database=YOUR_DATABASE;uid=YOUR_USERNAME;password=YOUR_PASSWORD;"))
{
connection.Open();
MySqlCommand userinfoCommand = new MySqlCommand("SELECT name, FROM table",connection);
using (MySqlDataReader reader = userinfoCommand.ExecuteReader())
{
while (reader.Read())
{
String name= reader.GetString("name");
}
connection.Close();
}
}
关于c# - 将 Windows 8 应用程序连接到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22462441/