Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting MySQL with Visual Studio C#

I'm new to MySQL Workbench and I'm trying to make a Timekeeping system. I'm wondering how to connect MySQL with Visual Studio C#?

like image 739
sean Avatar asked Jun 21 '11 03:06

sean


People also ask

Can I connect MySQL with C#?

Connect C# to MySQLAll the communication between a C# application and the MySQL server is routed through a MySqlConnection Object. So, before your application can communicate with the server, it must instantiate, configure, and open a MySqlConnection object.

Can I use MySQL in Visual Studio?

Many MySQL for Visual Studio features also require that MySQL Connector/NET be installed on the same host where you perform Visual Studio development. Connector/NET is a separate product. The options for installing MySQL for Visual Studio are: Using MySQL Installer (preferred): Download and execute the MySQL Installer.


2 Answers

you'll need a "connector/driver" to connect from .net to mysql, you can find the official .net connector from mysql here:

http://dev.mysql.com/downloads/connector/net/

the connector will install the MySql.Data library that you has classes to communicate to MySql (MySqlConnection, MySqlCommand, MySqlDataAdapter, etc)

like image 169
Jaime Avatar answered Sep 23 '22 15:09

Jaime


If you are working with MySQL for the first time in your PC, do these things.

  1. Install MySQL Server (Link here) - 28 MB
  2. Install MySQL ODBC Connector (Link here) - 3 MB

Now install SqlYog Community Edition. ( Link here ). You can manipulate your MySQL Databases using this.

Now in AppSettings of web.config, set two entries like this.

<configuration>
  <appSettings>
    <add key="ODBCDriver" value="Driver={MySQL ODBC 5.1 Driver};Server=localhost;"/>
    <add key="DataBaseDetails" value="Database=mydatabase;uid=root;pwd=;Option=3;"/>
  </appSettings>
</configuration>

And call it like in your MySQL class like this.

public string MyConnectionString 
{
    get
    {
        //return {MySQL ODBC 5.1 Driver};Server=localhost;Database=mydatabase;uid=root;pwd=;Option=3;
        return ConfigurationManager.AppSettings["ODBCDriver"]
            + ConfigurationManager.AppSettings["DataBaseDetails"];
    }
}

Now you can initialize your connection like this.

OdbcConnection connection = new OdbcConnection(MyConnectionString);

Namespace imported

using System.Data.Odbc;

Hope you get the idea.

like image 36
naveen Avatar answered Sep 23 '22 15:09

naveen