How to connect to MySQL database from Visual Studio VS2010 – problems with NET connectors
2011-04-18 09:40
771 查看
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 theChoose Data Source window it does not show the MySQL Database.The MySQL connector (an older version – 5.2.7.0) 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 fromhttp://www.mysql.com/downloads/connector/net/
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
use Connect to Database button
in the Choose Data Source windows select MySql Database and press Continue
VS2010 - MySQL DataSource
in the Add Connection window
set server name: 127.0.0.1 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
If you want to connect to a MySql database from a C# application (Windows or Web) you can use the next sequence:
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 theChoose Data Source window it does not show the MySQL Database.The MySQL connector (an older version – 5.2.7.0) 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 fromhttp://www.mysql.com/downloads/connector/net/
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
use Connect to Database button
in the Choose Data Source windows select MySql Database and press Continue
VS2010 - MySQL DataSource
in the Add Connection window
set server name: 127.0.0.1 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
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;"; try { //open the connection this.msqlConnection.Open(); //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 } finally { //always close the connection this.msqlConnection.Close(); }
相关文章推荐
- How to connect to MySQL database from Visual Studio VS2010 – problems with NET connectors
- How to connect to MySQL database from Visual Studio VS2010 – problems with NET connectors
- How to create a DLL with SWIG from Visual Studio 2010
- How to import existing projects into TFS Visual Studio with GIT ?
- How to Resolve the 'Error MSB6006 tsc.exe exited with code 2' build error in Visual Studio 2015
- How to Use the Visual Studio .NET Application Center Test
- How do I set up a Microsoft Visual Studio project to work with MATLAB Compiler 4.0?
- How to delete a team project from Team Foundation Service (tfs.visualstudio.com)
- How to call the EnumJobs function from a Visual Basic .NET application
- Top Ten Cool Features of Visual Studio .NET Help You Go From Geek to Guru
- OPD.NET Developer's Guide: Oracle Database 10g Developement with Visual Studio 2005 and the Oracle D
- How to Build libiconv with Microsoft Visual Studio
- How to debug c++ console application with argument in visual studio 2012
- The Complete Reference to Professional SOA with Visual Studio 2005 (C# & VB 2005) .NET 3.0
- 项目管理实践【六】自动同步数据库【Using Visual Studio with Source Control System to synchronize database automatically】
- Oracle:How to create physical standby database with 11g RMAN DUPLICATE FROM ACTIVE DATABASE [ID 747250.1]
- How to change the default browser to debug with in Visual Studio 2008?
- How to debug a Visual Studio .NET 2005 Add-In
- 项目管理实践【六】自动同步数据库【Using Visual Studio with Source Control System to synchronize database automatically】
- Migrating from VS 2005 to VS 2008 with Microsoft.VisualStudio.Shell references (译)