A connection between your C# application and a MySQL or MariaDB database is essential for any data-driven solution on the .NET platform. Whether you're creating a web app, desktop software, or backend service, a reliable database connection serves as the starting point for executing queries, handling data, and supporting dynamic functionality.

In this guide, you'll learn how to create a stable and efficient link between your .NET app and MySQL/MariaDB using dotConnect for MySQL, Devart's high-performance ADO.NET provider. We'll cover the full setup process—from initializing your project and installing the required NuGet package to configuring secure access with SSL/TLS. You'll also discover how to integrate seamlessly with modern ORM tools like Entity Framework Core using Scaffold-DbContext.

Why dotConnect for MySQL?

ORM support provided

Advanced ORM support

Fully supports EF Core, Dapper, NHibernate, LinqConnect, and other technologies for efficient data management.

Full ADO.NET compliance

Full ADO.NET compliance

Conforms to the latest ADO.NET standards and innovations for seamless integration with .NET applications.

Support for MySQL-specific data types

MySQL-specific data types

Offers many MySQL-specific features and fully supports all unique data types for accurate and complete data representation.

Secure connection ensured

Secure connection options

Provides robust security with support for SSL/SSH connections, connecting via proxy servers, embedded servers, and HTTP tunneling.

Integration with popular IDEs

IDE integration

Features native integration with Visual Studio and complete design-time support for accelerated development.

Priority support provided

Priority support & frequent updates

Includes priority support, detailed documentation, and regular updates for continuous improvement.

Download and activate dotConnect for MySQL

You can start using dotConnect for MySQL immediately with a 30-day free trial. Choose one of the following installation options:

30-day free trial version

dotnet add package Devart.Data.MySql
Install-Package Devart.Data.MySql

You can install the driver by using the Windows installer.

After you receive the license key, add it to your connection strings to connect to the data source.

Start using dotConnect for MySQL in your project today with a free trial

Connection strings

Property Meaning
Database Default database to be used after connecting.
Host Hostname or IP address of the MySQL server.
License Key Your license key. This is required only when using .NET Standard compatible assemblies.
Password Password for the user ID.
Port Port number on which the MySQL server is listening.
UserId User ID used to authenticate with MySQL.

Connect using C#

This example demonstrates a basic connection to a MySQL database. Depending on your requirements, you might want to add more functionality, such as executing queries or handling different types of exceptions.

static void Main(string[] args) {
  string connectionString = "" +
    "Host=127.0.0.1;" +
    "User Id=TestUser;" +
    "Password=TestPassword;" +
    "Database=TestDb;" +
    "License Key=**********";

  try {
    using (MySqlConnection connection = new MySqlConnection(connectionString)) {
      connection.Open();
      Console.WriteLine("Connection successful!");
    }
  }
  catch (Exception ex) {
    Console.WriteLine("An error occurred: " + ex.Message);
  }
}

The connection string contains the necessary information to connect to the database, such as the host, user ID, password, and database name.

Connect using the SSL/TLS connection

To establish the SSL connection to the MySQL database with the specified connection string parameters, modify your code as follows:

static void Main(string[] args) {
    string connectionString = "" +
      "Host=127.0.0.1;" +
      "User Id=TestUser;" +
      "Password=TestPassword;" +
      "Database=TestDb;" +
      "Protocol=SSL;" +
      "SSL CA Cert=file://C:\\CA-cert.pem;" +
      "SSL Cert=file://C:\\SSL-client-cert.pem;" +
      "SSL Key=file://C:\\SSL-client-key.pem;" +
      "License Key=**********";

    try {
        using (MySqlConnection connection = new MySqlConnection(connectionString)) {
            connection.Open();
            Console.WriteLine("SSL/TLS Connection successful!");
        }
    }
    catch (Exception ex) {
        Console.WriteLine("An error occurred: " + ex.Message);
    }
}

Connect using Server Explorer

You can also connect to the database using Visual Studio's built-in Data Explorer:

1. Go to the Server Explorer window, right-click Data Connections, and then select Add Connection.

Add a new connection

2. In the Add Connection dialog, click Change to specify the data source.

3. In the Change Data Source dialog, select dotConnect for MySQL from the list of installed data providers and click OK.

Select the data source

4. In the Add Connection dialog, select MySQL as the data source and provide your server credentials.

5. To verify the connection, click Test Connection.

Verify the connection

Once connected, you can use Data Explorer to browse tables, run queries, and manage data directly within Visual Studio.

Browse tables

Connect with EF Core using Entity Developer

Quickly build your EF Core model from your MySQL database using the visual Entity Developer wizard.

For a complete guide, see the full step-by-step instructions.

Check the created model

All steps in this guide are performed in Visual Studio, but if you prefer VS Code, JetBrains Rider, or another similar tool, you can use ED as a standalone application to work with the models efficiently.

To create a simple example that checks a database connection using Entity Framework Core (EF Core) with MySQL, follow these steps.

class Program {
  static void Main(string[] args) {
    // Assuming the SakilaContext class is generated by Entity Developer
    using (var context = new SakilaContext()) {
      try {
        context.Database.CanConnect();
        Console.WriteLine("Connection successful!");
      }
      catch (Exception ex) {
        Console.WriteLine("An error occurred: " + ex.Message);
      }
    }
  }
}

Connect with EF Core using Scaffold-DbContext

To connect to the MySQL database using Entity Framework Core (EF Core) and scaffold its context, follow this step-by-step instruction. This process generates the necessary EF Core model classes and a DbContext based on the existing MySQL database schema.

Install the required NuGet packages

In the Solution Explorer, right-click the project and select Manage NuGet Packages. Then, open the Package Manager Console. To do this, go to Tools > NuGet Package Manager > Package Manager Console

Scaffold the DbContext

In the Package Manager Console, run the following command to scaffold the DbContext and entity classes from the MySQL database. Replace the connection string with your relevant connection details.

Scaffold-DbContext "Host=127.0.0.1;Port=3306;User Id=TestUser;Password=TestPassword;Database=sakila;License Key=**********;" Devart.Data.MySql.EFCore -OutputDir Models
Note
The -OutputDir Models specifies the output directory where the generated model classes will be placed.

After running the scaffold command, EF Core generates the DbContext class and entity classes in the specified output directory (e.g., Models). Review the generated code to ensure it matches your database schema.

Video tutorial: How to connect .NET console application to a MySQL database

Conclusion

This article provides detailed guides on connecting a .NET application to MySQL or MariaDB databases in several ways. It explores various connection methods, including secure SSL/TLS connections and using Entity Framework Core with
Scaffold-DbContext.

When developing applications, you can make the most of Devart dotConnect for MySQL's solutions to achieve a much faster, smoother, and more straightforward performance of all tasks. Download a free trial to experience seamless database connectivity, performance optimizations, and robust security for your .NET projects!

FAQ

How do you install and activate dotConnect for MySQL in a .NET project?
Install dotConnect for MySQL via the EXE installer or by adding the Devart.Data.MySql NuGet package to your project, then obtain your personal activation key from your Devart Customer Portal and include it in the connection string via the License Key parameter for a working connection.
How do you create a connection to MySQL using dotConnect in C#?
Define a connection string that includes host, user ID, password, database, and the License Key value, then create a MySqlConnection instance with this string and call Open() for it inside a try-catch block to test and handle connection errors.
How do you enable SSL/TLS for secure MySQL connections with dotConnect?
Add Protocol=SSL and specify the SSL CA Cert, SSL Cert, and SSL Key file paths in the connection string, then open the MySqlConnection connection to establish an encrypted SSL/TLS connection.
Can you connect to MySQL using Entity Framework Core and dotConnect?
Yes, you can either use Entity Developer to visually create an EF Core model from the database or run Scaffold-DbContext with a dotConnect connection string (including License Key) to generate the DbContext and entity classes.
Is it possible to connect to MySQL using Visual Studio Server Explorer with dotConnect?
Yes, in Visual Studio Server Explorer, you add a new Data Connection, choose dotConnect for MySQL as the data provider, enter your MySQL server credentials, test the connection, and then browse and manage data directly from the IDE.

Dereck Mushingairi

I'm a technical content writer who loves turning complex topics — think SQL, connectors, and backend chaos–into content that actually makes sense (and maybe even makes you smile). I write for devs, data folks, and curious minds who want less fluff and more clarity. When I'm not wrangling words, you'll find me dancing salsa, or hopping between cities.

Try the 30-day trial of the full product. No limits. No card required Start free trial