zoukankan      html  css  js  c++  java
  • 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 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

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

  • 相关阅读:
    Ubuntu 11.04 安装后要做的20件事情
    Net 服务命令行参考之一
    Openerp约束句型
    Ubuntu进入Shell
    postgreSql基础命令及linux下postgreSql命令
    解决中文乱码的问题
    An error occured while handling a json request
    Java Socket编程
    CentOS 7 中 Docker 的安装和卸载
    Spring Boot整合shiro-登录认证和权限管理
  • 原文地址:https://www.cnblogs.com/googlegis/p/2978853.html
Copyright © 2011-2022 走看看