I am getting the above error when I execute the code -
MySqlConnection mysqlConn=new MySqlConnection(\"server=127.0.0.1;uid=pankaj;port=3306;pwd=master;datab
using System;
using System.Linq;
using MySql.Data.MySqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// add here your connection details
String connectionString = "Server=localhost;Database=database;Uid=username;Pwd=password;";
try
{
MySqlConnection connection = new MySqlConnection(connectionString);
connection.Open();
Console.WriteLine("MySQL version: " + connection.ServerVersion);
connection.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.ReadKey();
}
}
}
make sure your database server is running if its not running then its not able to make connection and bydefault mysql running on 3306 so don't need mention port if same in case of port number is different then we need to mention port
In my case the hosting had a whitelist for remote connection to MySql. Try to add your IP to whitelist in hosting admin panel.
for users running VS2013
In windows 10.
Check if apache service is running. since it gets replaced with World wide web service.
run netstat -n to check this.
stop the service. start apache. restart the service.
Try this:
MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
conn_string.Server = "127.0.0.1";
conn_string.Port = 3306;
conn_string.UserID = "root";
conn_string.Password = "myPassword";
conn_string.Database = "myDB";
MySqlConnection MyCon = new MySqlConnection(conn_string.ToString());
try
{
MyCon.Open();
MessageBox.Show("Open");
MyCon.Close();
MessageBox.Show("Close");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Sometime the problem could be on your windows firewall, make sure your server allow access to all port associated with your mysql database.
Sometimes spacing and Order of parameters in connection string matters (based on personal experience and a long night :S)
So stick to the standard format here
Server=myServerAddress; Port=1234; Database=myDataBase; Uid=myUsername; Pwd=myPassword;