c# - 将 Windows 8 应用程序连接到 MySQL

标签 c# mysql windows-8.1

我正在使用 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/

相关文章:

c# - Double.Parse ("NaN") 是否正确解析?

C# 从数组中保存图像

c# - 在另一个创建的方法中停止对象的正确方法是什么?

时间:2019-05-17 标签:c#mysqlconnectionstring问题

MySQL仅输出n行并将其余行分组为第n + 1行

python - Tkinter 理解 after()

c++ - memcpy 访问冲突错误

c# - 具有可选参数和 new() 约束的构造函数?

mysql - 过滤 Sql 中的重复结果

javascript - 单击选择后,cordova Windows8.1 上的 console.log/info/assert undefined