2

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.

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.

1 Answer 1

2

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

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.

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.

To install MySql Engine see 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

How to use WorkBench, just google some training videos on the internet.

Sign up to request clarification or add additional context in comments.

11 Comments

Ok, So i have already also tried Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; this before and it is still not working correctly.
@zeddosx Is this database hosted on the shared cloud?
Alehosanini I got this database for my website, so it is on Web hosting. Also it works on websites with php.
@zeddosx 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 Id to their firewall to accept your connection.
Ok, so it is most likely that it is problem on my hosting side?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.