简体   繁体   English

如何使用 c# 连接到 phpmyadmin mysql 数据库(使用 Z9EDB3C572B56B915642AF65Data948051);

[英]How to connect to phpmyadmin mysql database using c# (using Mysql.Data.MysqlClient;)

I am trying to connect to the MySQL database using this code client_v is the name of the table I want to get data from.我正在尝试使用此代码连接到 MySQL 数据库 client_v 是我要从中获取数据的表的名称。

I went through a lot of similar questions here but I really haven't found an answer to this question.我在这里遇到了很多类似的问题,但我真的没有找到这个问题的答案。

using MySql.Data.MySqlClient;

private void Form1_Load(object sender, EventArgs e) {
  try {
    string connetionstring = "Server=a029um.forpsi.com;Uid=userlogin;Pwd=userpassword";
    string mysql = "SELECT * FROM dbname.cilent_v";
    MySqlConnection conn = new MySqlConnection(connetionstring);
    MySqlCommand command = new MySqlCommand(mysql, conn);
    conn.Open();
    MySqlDataAdapter dtb = new MySqlDataAdapter();
    dtb.SelectCommand = command;

    DataTable dtable = new DataTable();
    dtb.Fill(dtable);
    dataGridView1.DataSource = dtb;
  } catch(Exception ex) {
    MessageBox.Show(ex.Message);
  }
}

The error it is returning:它返回的错误:

Unable to connect to any of the specified MySQL hosts.无法连接到任何指定的 MySQL 主机。

Initially, I can see that you miss the Database part in your connection string.最初,我可以看到您错过了连接字符串中的数据库部分。

Be sure that your connection string is valid (including valid user name and password)确保您的连接字符串有效(包括有效的用户名和密码)

For MySql, you can find here all example list对于 MySql,您可以在此处找到所有示例列表

https://www.connectionstrings.com/mysql/ https://www.connectionstrings.com/mysql/

I also noticed that your server is hosted on the cloud (most likely shared hosting) so you need to contact the service provider and ask them to add your computer's public IP to their firewall to accept your connection.我还注意到您的服务器托管在云上(很可能是共享托管),因此您需要联系服务提供商并要求他们将您计算机的公共 IP 添加到他们的防火墙以接受您的连接。

The better practice, for the development environment, install the MySql engine and WorkBench on your local PC, and finally when you reach the point to deploy, that time change the connection string to connect to the cloud-hosted Database.更好的做法是,对于开发环境,在本地 PC 上安装MySql引擎和WorkBench ,最后当您到达部署点时,更改连接字符串以连接到云托管数据库。

To install MySql Engine see https://dev.mysql.com/doc/mysql-installation-excerpt/5.7/en要安装MySql 引擎,请参阅https://dev.mysql.com/doc/mysql-installation-excerpt/5.7/en

To install WorkBench (The database management studio) see https://dev.mysql.com/doc/workbench/en/wb-installing-windows.html要安装WorkBench (数据库管理工作室),请参阅https://dev.mysql.com/doc/workbench/en/wb-installing-windows.ZFC35FDC70D5FC69D269883A822C7A5E3

How to use WorkBench, just google some training videos on the internet.如何使用 WorkBench,只需 google 一些互联网上的培训视频。

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

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