Connect Windows 8 app to MySQL

空扰寡人 提交于 2019-12-11 12:06:36

问题


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.

This is my connection string:

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

The code looks like:

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

and then open my connection like that.

The error that I get is

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?


回答1:


I can not see the rest of your code but if I was doing a connection the correct way I will do it is:

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



回答2:


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


来源:https://stackoverflow.com/questions/22462441/connect-windows-8-app-to-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!