I can not connect to MySQL from C #

1

I'm trying to connect to a MySQL database in C #, and my code has been working perfectly since I started, but sometimes I know it's giving me this error like, every 2 days, sometimes it connects sometimes not , I have 2 DB in different servers and when I try to connect to the second server it always works, what happens?

this is the error

  

An exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll but was not handled in the user code

     

Additional information: can not connect to any of the specified MySQL hosts.

This is my code

    MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
    conn_string.Server = Conexion.Server;
    conn_string.Port = Conexion.PORT;
    conn_string.UserID = Conexion.UID;
    conn_string.Password = Conexion.PASSWORD;
    conn_string.Database = Conexion.Database;
    MySqlConnection SqlCon = new MySqlConnection(conn_string.ToString());
    SqlCon.Open();
    if (SqlCon.State == ConnectionState.Open)
    {
        toolStripStatusLabel_Con.Text = "Conectado";
        toolStripStatusLabel_Con.ForeColor = System.Drawing.Color.Green;
        SqlCon.Close();
    }
    else
    {
        toolStripStatusLabel_Con.Text = "Desconectado";
        toolStripStatusLabel_Con.ForeColor = System.Drawing.Color.Red;
        SqlCon.Close();
    }
    
asked by Alex Alfonso 04.11.2017 в 09:16
source

1 answer

0

Check that the connection string is correct before creating the object. With the connection string try a external application if you have the same problem.

If with the same code you can access one server and not another, it can also be a network connectivity problem.

    
answered by 19.03.2018 в 09:53