Azure Database for MySQL is a fully managed service that brings the reliability and scalability of the Azure cloud to one of the most popular open-source databases in the world. Whether you're building web apps, APIs, or data-driven services, MySQL in Azure offers a robust backend that integrates easily with the .NET technology.
This tutorial describes how to connect to an Azure MySQL database from a .NET application and perform basic CRUD operations using C# code. To simplify the task, we will use dotConnect for MySQL, a powerful data provider that enables direct connection to MySQL and MariaDB in the cloud.
Why dotConnect for MySQL?
Advanced ORM support
Fully supports EF Core, Dapper, NHibernate, LinqConnect, and other technologies for efficient data management.
Full ADO.NET compliance
Conforms to the latest ADO.NET standards and innovations for seamless integration with .NET applications.
MySQL-specific data types
Offers many MySQL-specific features and fully supports all unique data types for accurate and complete data representation.
Secure connection options
Provides robust security with support for SSL/SSH connections, connecting via proxy servers, embedded servers, and HTTP tunneling.
IDE integration
Features native integration with Visual Studio and complete design-time support for accelerated development.
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:
Connect to Azure Database for MySQL with Data Explorer
Let us connect to MySQL in Azure with the help of the built-in Data Explorer. Click Tools, then select Connect to Database, and choose MySQL as the data source.
Enter your server details and credentials. Click Connect.
Once connected, you can browse tables, execute queries, and manage data directly within the Data Explorer.
Connect and retrieve data from the database
Let us establish a connection with the Azure MySQL database and then run a simple query to pull records from the Actor table. It confirms the connection is working and the data is accessible.
Create the DatabaseConnection.cs class
The DatabaseConnection.cs class keeps connection details separate for better maintainability.
Replace the placeholders in the connection string with your actual MySQL database credentials. If you own a paid license of dotConnect for MySQL, include the license key in the connection strings.
using Devart.Data.MySql;
publicsealedclassProgram
{
publicstaticvoidMain(string[] args)
{
try
{
using (MySqlConnection connection = DatabaseConnection.CreateConnection())
{
connection.Open();
Console.WriteLine("Connection successful!");
string insertSql = "INSERT INTO actor(first_name, last_name) VALUES(@first, @last)";
using (MySqlCommand insertCmd = new MySqlCommand(insertSql, connection))
{
insertCmd.Parameters.AddWithValue("@first", "TestFirst");
insertCmd.Parameters.AddWithValue("@last", "TestLast");
insertCmd.ExecuteNonQuery();
}
long newActorId = 0;
using (MySqlCommand idCmd = new MySqlCommand("SELECT LAST_INSERT_ID()", connection))
{
object result = idCmd.ExecuteScalar();
if (result != null && result != DBNull.Value)
{
newActorId = Convert.ToInt64(result);
}
}
Console.WriteLine("Inserted test actor (actor_id: " + newActorId + "): TestFirst TestLast");
}
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}
We can see the results in our application:
Update rows in Azure Database for MySQL
Next, we want to update an existing record by changing the actor's first and last name. The following code shows how to build and run an UPDATE command to modify data already stored in the database.
using Devart.Data.MySql;
publicsealedclassProgram
{
publicstaticvoidMain(string[] args)
{
try
{
using (MySqlConnection connection = DatabaseConnection.CreateConnection())
{
connection.Open();
Console.WriteLine("Connection successful!");
string updateSql = "UPDATE actor SET first_name = @first, last_name = @last WHERE actor_id = @id";
using (MySqlCommand updateCmd = new MySqlCommand(updateSql, connection))
{
updateCmd.Parameters.AddWithValue("@first", "Michael");
updateCmd.Parameters.AddWithValue("@last", "Anderson");
updateCmd.Parameters.AddWithValue("@id", 205);
int rowsAffected = updateCmd.ExecuteNonQuery();
if (rowsAffected > 0)
{
Console.WriteLine("Updated actor: Michael Anderson");
string selectSql = "SELECT actor_id, first_name, last_name FROM actor WHERE actor_id = @id";
using (MySqlCommand selectCmd = new MySqlCommand(selectSql, connection))
{
selectCmd.Parameters.AddWithValue("@id", 205);
using (MySqlDataReader reader = selectCmd.ExecuteReader())
{
if (reader.Read())
{
Console.WriteLine($"actor_id: {reader["actor_id"]}, first_name: {reader["first_name"]}, last_name: {reader["last_name"]}");
}
}
}
}
else
{
Console.WriteLine("No actor found with actor_id: 205");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}
We can see the results in our application:
Delete rows from Azure Database for MySQL
Finally, let us see how to delete the record. We are going to remove the actor we have just added to the database using the DELETE command with a condition.
using Devart.Data.MySql;
publicsealedclassProgram
{
publicstaticvoidMain(string[] args)
{
try
{
using (MySqlConnection connection = DatabaseConnection.CreateConnection())
{
connection.Open();
Console.WriteLine("Connection successful!");
string deleteSql = "DELETE FROM actor WHERE actor_id = @id";
using (MySqlCommand deleteCmd = new MySqlCommand(deleteSql, connection))
{
deleteCmd.Parameters.AddWithValue("@id", 205);
int rowsAffected = deleteCmd.ExecuteNonQuery();
if (rowsAffected > 0)
{
Console.WriteLine("Deleted actor with actor_id: 205");
}
else
{
Console.WriteLine("No actor found with actor_id: 205");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}
The output in the application proves that the operation is successful.
Conclusion
This tutorial shows how to perform all the basic database operations against the cloud MySQL database in Azure from a .NET application. We have successfully connected, queried, inserted, updated, and deleted records, which are essential tasks for almost any application. From that, you can move forward to more advanced techniques, utilizing parameterized queries, transactions, stored procedures, and other features. In any case, you may not worry about connecting to MySQL and MariaDB in the Azure cloud, as dotConnect for MySQL will grant a smooth and secure connection simply and efficiently.
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.
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.