• 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

  • 相关阅读:
    c#构造函数对string类型赋初值
    个人计算机管理
    .net Core2建立MVC网站,部署
    运行或开发.NET Core 的先决条件(支持项目、依赖项)
    在Windows下不使用密码远程登陆Linux
    在Linux下不使用密码远程登陆其他Linux
    如何查看MySQL中每张表占用的空间大小
    pdo如何防止 sql注入
    受教了,memcache比较全面点的介绍,受益匪浅,适用memcached的业务场景有哪些?memcached的cache机制是怎样的?在设计应用时,可以通过Memcached缓存那些内容?
    LVS原理详解及部署之二:LVS原理详解(3种工作方式8种调度算法)
  • 原文地址:https://www.cnblogs.com/lhard/p/2284478.html
Copyright © 2020-2023  润新知