Visual Studio .NET - Connecting visual studio 2010 to mySql database

Asked By Bigboy on 03-Nov-11 08:11 AM
How are you. I am failing to connect my visual studio 2010 program to mySql database.
I once used the option for right clicking the project name whilst in visual studio 2010, then clicking on properties, then references. I then clicked on add, but could not find the option for mySql data, as it is not available.
How do I go about this problem. Your help will be greatly appreciated. Thank you.
Bigboy
Riley K replied to Bigboy on 03-Nov-11 08:17 AM


In order to connect to a MySql database from VS2010 you need to
  • download the latest version of the MySql Connector/NET from http://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)
http://www.itcsolutions.eu/wp-content/uploads/2010/09/VS2010_ServerExplorer.jpg

  • use Connect to Database button
  • in the Choose Data Source windows select MySql Database and press Continue
http://www.itcsolutions.eu/wp-content/uploads/2010/09/VS2010_MySql_DataSource.jpg

  • 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
http://www.itcsolutions.eu/wp-content/uploads/2010/09/VS2010_MySql_Connection.jpg

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


Regards
Suchit shah replied to Bigboy on 03-Nov-11 08:17 AM

You can either use ODBC or MySql Client for .NET.

1. Download MySql Client dll.

2. Add the dll to your project.

3. Add connection string to web.config

<connectionStrings>
<add name="Connstring" connectionString="server=localhost; userid=uid;password=pwd;pooling=yes;Database=DB" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
<system.data>
<DbProviderFactories>
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=5.0.2.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
</DbProviderFactories>
</system.data>

thatt's it you can follow same sqlclient connection.

dipa ahuja replied to Bigboy on 03-Nov-11 08:23 AM
  • 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)
  • use Connect to Database button
  • in the Choose Data Source windows select MySql Database and press Continue
    • 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
    http://www.itcsolutions.eu/2010/09/09/how-to-connect-to-mysql-database-from-visual-studio-vs2010-problems-with-net-connectors/

Kirtan Patel replied to Bigboy on 03-Nov-11 08:32 AM
Untitled document
you can do it like below
 
Step1 :
----------
Download MySQlConnector for .net from below link
 
http://dev.mysql.com/downloads/connector/net/5.1.html
 
Step 2 : Write code like below
-------------------------------
 
/* Code to Fill the DataGridView with Data from MySql */
 
MySqlConnection con = new MySqlConnection("Connection String");
con.Open();
MySqlCommand comm = new MySqlCommand("select * from TableName",con);
MySqlDataAdatapter da = new MySqlDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill()
con.Close();
dataGridView1.DataSource = dt;
 
Also Add Corresponding Namespaces as needed
 
 
Reena Jain replied to Bigboy on 03-Nov-11 09:08 AM
Hi,

To connect to MySQL server through MyODBC using ODBC.NET.
  1. Import the System.Data.Odbc namespace (ODBC.NET) to your application using the following statement:
      using System.Data.Odbc; 
    
  2. Once the namespace is imported in your application, you can create a simple class and establish a connection to MySQL server through MyODBC using an OdbcConnection object.
    string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
         "SERVER=servername;" +
         "DATABASE=test;" +
         "UID=reena;" +
         "PASSWORD=jain;" +
         "OPTION=3";
    OdbcConnection MyConnection = new OdbcConnection(MyConString);
    MyConnection.Open();
You one need to give you live server name or IP address to connect with live database

Hope this will help you
Bigboy replied to Reena Jain on 03-Nov-11 02:46 PM
Thank you very much. All helped