简体   繁体   English

将Windows 8应用连接到MySQL

[英]Connect Windows 8 app to MySQL

I am making my first Windows 8 app using C#. 我正在使用C#制作我的第一个Windows 8应用程序。 I have a MySQL Database that I would like to connect to. 我有一个要连接的MySQL数据库。 I have done this before with windows forms and everything went smoothly. 我以前使用Windows窗体已经做到了,一切都进行得很顺利。 However with the windows 8 app it won't connect. 但是,使用Windows 8应用程序将无法连接。

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 MySql.Data.RT.DLL中发生类型为'MySql.Data.MySqlClient.MySqlException'的异常,但未在用户代码中处理

Can anyone explain why this is and what I am doing wrong? 谁能解释这是为什么以及我在做什么错?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM