将 Windows 8 应用程序连接到 MySQL

问题描述:

我正在使用 C# 制作我的第一个 Windows 8 应用程序.我有一个想要连接的 MySQL 数据库.我以前用 Windows 窗体做过这个,一切都很顺利.但是,使用 Windows 8 应用程序时,它无法连接.

I am making my first Windows 8 app using C#. I have a MySQL Database that I would like to connect to. I have done this before with windows forms and everything went smoothly. However with the windows 8 app it won't connect.

这是我的连接字符串:

string myConnectionString = "Server=mysql9.000webhost.com; Database=a2236339_snooker; Uid=a2236339_joe; password=TeamPr0ject;";

代码如下:

MySqlConnection connection = new MySqlConnection(myConnectionString);
connection.Open();

然后像这样打开我的连接.

and then open my connection like that.

我得到的错误是

MySql.Data.RT.DLL 中发生了MySql.Data.MySqlClient.MySqlException"类型的异常,但未在用户代码中处理

An exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.RT.DLL but was not handled in user code

谁能解释为什么会这样以及我做错了什么?

Can anyone explain why this is and what I am doing wrong?

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();
     }
}