6

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#?

1

5 Answers 5

9

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)

2
  • 1
    Once you have installed the .net connector, used the mysql.data library, and compiled the .exe, if you execute that .exe on a machine that has not installed the mysql .net connector, will it work? Oct 1, 2014 at 23:39
  • 1
    A word of warning though, this connector is licensed with GPL, effectively forcing your project to be licensed with it as well and if it's a commercial project developed for another company, they might not be too happy to have it on GPL.
    – jahu
    May 11, 2015 at 9:44
6

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.

5

The easiest way is to use NuGet to get the .Net connector for MySQL:

enter image description here

Once you install the MySql.Data package, you can do something like this:

using (var connection = new MySqlConnection("Server=localhost;Database=MyDatabaseName;Uid=root;Pwd=;"))
using (var command = connection.CreateCommand()) {
    connection.Open();
    command.CommandText = "select id, name from widgets";

    using (var reader = command.ExecuteReader())
        while (reader.Read())
            Console.WriteLine(reader.GetString(0) + ": " + reader.GetString(1));
}
2

Try this website:

http://www.connectionstrings.com/mysql#p34

Set up your connection string and then the rest should just work as if you were calling a SQLServer database.

Good luck.

2

You can connect to MySQL using dotConnect for MySQL.

more information

Not the answer you're looking for? Browse other questions tagged or ask your own question.