Categories
c# mysql mysql-connector nuget-package

How to connect to MySQL Database?

98

New to C# programming, I’d like to be able to access MySQL Databases.

I know MySQL connector/NET and MySQL for Visual Studio are required for C# development.
Do I need to install them into my app?
Is it possible I can just release the connector DLL with the program?

Update:
Are both of them required for the end-user or just the connector?
Is there anything else they would need?

4

  • 4

    Yes, all you need to do is package the DLL with you program, it should already be in your output debug/release folder when you run your program.

    – ohmusama

    Feb 7, 2014 at 2:15


  • Note: MySQL for Visual Studio don’t work in VS2010E

    Aug 19, 2015 at 18:47

  • As for the above note by @EnriqueSanMartín Version 2.0 and Version 1.2 Support for Microsoft Visual Studio 2010 was removed.

    Aug 11, 2017 at 4:36


  • See here if you need to connect to MySQL over SSH rather than directly on port 3306 stackoverflow.com/questions/10806799/…

    Dec 3, 2020 at 8:43

55

You can use Package Manager to add it as a package and it is the easiest way. You don’t need anything else to work with a MySQL database.

Or you can run this command in Package Manager Console:

PM> Install-Package MySql.Data

NuGet MySql.Data

2

  • If you search it through “Manage NuGet Packages” window, keep in mind that package name is case sensitive. Maybe not such an obvious thing for someone coming from package managers with lowercase names.

    – Dimitry K

    Apr 6, 2015 at 16:08

  • 1

    Thanks, it is very small detail, so if anyone wants to check complete step by step procedure to connect with MySQL Check qawithexperts.com/article/c-sharp/…

    – Jyoti

    Apr 19 at 15:56

18

You must to download MySQLConnection NET from here.

Then you need add MySql.Data.DLL to MSVisualStudio like this:

  1. Open menu project
  2. Add
  3. Reference
  4. Browse to C:\Program Files (x86)\MySQL\MySQL Connector Net 8.0.12\Assemblies\v4.5.2
  5. Add MySql.Data.dll

If you want to know more visit: enter link description here

To use in the code you must import the library:

using MySql.Data.MySqlClient;

An example with connectio to Mysql database (NO SSL MODE) by means of Click event:

using System;
using System.Windows;
using MySql.Data.MySqlClient;


namespace Deportes_WPF
{

public partial class Login : Window
{
    private MySqlConnection connection;
    private string server;
    private string database;
    private string user;
    private string password;
    private string port;
    private string connectionString;
    private string sslM;

    public Login()
    {
        InitializeComponent();

        server = "server_name";
        database = "database_name";
        user = "user_id";
        password = "password";
        port = "3306";
        sslM = "none";

        connectionString = String.Format("server={0};port={1};user id={2}; password={3}; database={4}; SslMode={5}", server, port, user, password, database, sslM);

        connection = new MySqlConnection(connectionString);
    }

    private void conexion()
    {
        try
        {
            connection.Open();

            MessageBox.Show("successful connection");

            connection.Close();
        }
        catch (MySqlException ex)
        {
            MessageBox.Show(ex.Message + connectionString);
        }
    }

    private void btn1_Click(object sender, RoutedEventArgs e)
    {
        conexion();
    }
  }

}

1

  • I like the method name conexion

    – Arkyo

    Feb 12 at 3:31