• 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 –  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

    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: 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

    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();
    }

    转自:http://www.itcsolutions.eu

  • 相关阅读:
    新买的电脑桌面只有回收站该做些什么
    不安装oracle客户端也可以使用pl/sql developer
    Win7上安装Oracle数据库
    忘记oracle的sys用户密码怎么修改
    UML中类之间的关系
    JAVAEE 是什么,如何获取各种规范jar包及各种规范的jar包源码
    PL/SQL Developer使用技巧、快捷键
    Windows 7上安装Microsoft Loopback Adapter(微软环回网卡)
    超棒的30款JS类库和工具
    HTTP协议
  • 原文地址:https://www.cnblogs.com/lhard/p/2284478.html
Copyright © 2020-2023  润新知