How to connect to MySQL database from Visual Studio VS2010 – problems with NET connectors

I have developed a Windows Application in Visual Studio 2008 some years ago and now it has been updated in Visual Studio 2010.

The application connects to a MySQL database and after porting it to VS2010, the IDE does not show the connection to the MySQL in Server Explorer. Also when trying to add the data connection, in the Choose Data Source window it does not show the MySQL Database.The MySQL connector (an older version – was installed and the reference to MySql.Data namespace was added. There were no problems compiling the project, just VS2010 could not see the connector when trying to create a new Data connection.

This is a compatibility problem between VS2010 and MySQL connector. The same problem has been reported also for the MySql Connector/Net 6.2.2.

The problem seems that has been solved with the release of the MySql Connector/Net 6.3.4. But this version has another problem.The MySQL connector does not work on Windows XP, only on Windows 7. In my case I could not install the connector (the installation wizard failed). So solving this problem you could not install the application on Windows XP because it will not find the required connector. This problem has another solution: How to deploy .NET applications that use modules with different version than the development ones

So, in order to connect to a MySql database from VS2010 you need to

  • download the latest version of the MySql Connector/NET from
  • install the connector (if you have an older version you need to remove it from Control Panel -> Add / Remove Programs)
  • open Visual Studio 2010
  • open Server Explorer Window (View -> Server Explorer)
Visual Studio 2010 - Server Explorer window
Visual Studio 2010 – Server Explorer window
  • use Connect to Database button
  • in the Choose Data Source windows select MySql Database and press Continue
VS2010 - MySQL DataSource
VS2010 – MySQL DataSource
  • in the Add Connection window
    • set server name: or localhost for MySql server running on local machine or an IP address for a remote server
    • username and password
    • if the the above data is correct and the connection can be made, you have the possibility to select the database
VS2010 MySQL Connection properties
VS2010 MySQL Connection properties

If you want to connect to a MySql database from a C# application (Windows or Web) you can use the next sequence:

//define the connection reference and initialize it
MySql.Data.MySqlClient.MySqlConnection msqlConnection = null;
msqlConnection = new MySql.Data.MySqlClient.MySqlConnection(“server=localhost;user id=UserName;Password=UserPassword;database=DatabaseName;persist security info=False”);
    //define the command reference
MySql.Data.MySqlClient.MySqlCommand msqlCommand = new MySql.Data.MySqlClient.MySqlCommand();
    //define the connection used by the command object
msqlCommand.Connection = this.msqlConnection;
    //define the command text
msqlCommand.CommandText = "SELECT * FROM TestTable;";
    //open the connection
    //use a DataReader to process each record
    MySql.Data.MySqlClient.MySqlDataReader msqlReader = msqlCommand.ExecuteReader();
    while (msqlReader.Read())
        //do something with each record
catch (Exception er)
    //do something with the exception
    //always close the connection