问题
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