Events
Mar 31, 11 PM - Apr 2, 11 PM
The ultimate Microsoft Fabric, Power BI, SQL, and AI community-led event. March 31 to April 2, 2025.
Register todayThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
This quickstart demonstrates how to connect to an Azure Database for MySQL Flexible Server instance by using a C# application. It shows how to use SQL statements to query, insert, update, and delete data in the database.
For this quickstart you need:
If you don't have an Azure subscription, create an Azure free account before you begin. Currently, with an Azure free account, you can try Azure Database for MySQL - Flexible Server free for 12 months. For more information, see Use an Azure free account to try Azure Database for MySQL - Flexible Server for free.
At a command prompt, run:
mkdir AzureMySqlExample
cd AzureMySqlExample
dotnet new console
dotnet add package MySqlConnector
Get the connection information needed to connect to the Azure Database for MySQL Flexible Server instance. You need the fully qualified server name and login credentials.
Use the following code to connect and load the data by using CREATE TABLE
and INSERT INTO
SQL statements. The code uses the methods of the MySqlConnection
class:
Replace the Server
, Database
, UserID
, and Password
parameters with the values that you specified when you created the server and database.
using System;
using System.Threading.Tasks;
using MySqlConnector;
namespace AzureMySqlExample
{
class MySqlCreate
{
static async Task Main(string[] args)
{
var builder = new MySqlConnectionStringBuilder
{
Server = "YOUR-SERVER.mysql.database.azure.com",
Database = "YOUR-DATABASE",
UserID = "USER",
Password = "PASSWORD",
SslMode = MySqlSslMode.Required,
};
using (var conn = new MySqlConnection(builder.ConnectionString))
{
Console.WriteLine("Opening connection");
await conn.OpenAsync();
using (var command = conn.CreateCommand())
{
command.CommandText = "DROP TABLE IF EXISTS inventory;";
await command.ExecuteNonQueryAsync();
Console.WriteLine("Finished dropping table (if existed)");
command.CommandText = "CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER);";
await command.ExecuteNonQueryAsync();
Console.WriteLine("Finished creating table");
command.CommandText = @"INSERT INTO inventory (name, quantity) VALUES (@name1, @quantity1),
(@name2, @quantity2), (@name3, @quantity3);";
command.Parameters.AddWithValue("@name1", "banana");
command.Parameters.AddWithValue("@quantity1", 150);
command.Parameters.AddWithValue("@name2", "orange");
command.Parameters.AddWithValue("@quantity2", 154);
command.Parameters.AddWithValue("@name3", "apple");
command.Parameters.AddWithValue("@quantity3", 100);
int rowCount = await command.ExecuteNonQueryAsync();
Console.WriteLine(String.Format("Number of rows inserted={0}", rowCount));
}
// connection will be closed by the 'using' block
Console.WriteLine("Closing connection");
}
Console.WriteLine("Press RETURN to exit");
Console.ReadLine();
}
}
}
Having any issues? Let us know.
Use the following code to connect and read the data by using a SELECT
SQL statement. The code uses the MySqlConnection
class with methods:
Replace the Server
, Database
, UserID
, and Password
parameters with the values that you specified when you created the server and database.
using System;
using System.Threading.Tasks;
using MySqlConnector;
namespace AzureMySqlExample
{
class MySqlRead
{
static async Task Main(string[] args)
{
var builder = new MySqlConnectionStringBuilder
{
Server = "YOUR-SERVER.mysql.database.azure.com",
Database = "YOUR-DATABASE",
UserID = "USER@YOUR-SERVER",
Password = "PASSWORD",
SslMode = MySqlSslMode.Required,
};
using (var conn = new MySqlConnection(builder.ConnectionString))
{
Console.WriteLine("Opening connection");
await conn.OpenAsync();
using (var command = conn.CreateCommand())
{
command.CommandText = "SELECT * FROM inventory;";
using (var reader = await command.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
Console.WriteLine(string.Format(
"Reading from table=({0}, {1}, {2})",
reader.GetInt32(0),
reader.GetString(1),
reader.GetInt32(2)));
}
}
}
Console.WriteLine("Closing connection");
}
Console.WriteLine("Press RETURN to exit");
Console.ReadLine();
}
}
}
Use the following code to connect and read the data by using an UPDATE
SQL statement. The code uses the MySqlConnection
class with method:
Replace the Server
, Database
, UserID
, and Password
parameters with the values that you specified when you created the server and database.
using System;
using System.Threading.Tasks;
using MySqlConnector;
namespace AzureMySqlExample
{
class MySqlUpdate
{
static async Task Main(string[] args)
{
var builder = new MySqlConnectionStringBuilder
{
Server = "YOUR-SERVER.mysql.database.azure.com",
Database = "YOUR-DATABASE",
UserID = "USER",
Password = "PASSWORD",
SslMode = MySqlSslMode.Required,
};
using (var conn = new MySqlConnection(builder.ConnectionString))
{
Console.WriteLine("Opening connection");
await conn.OpenAsync();
using (var command = conn.CreateCommand())
{
command.CommandText = "UPDATE inventory SET quantity = @quantity WHERE name = @name;";
command.Parameters.AddWithValue("@quantity", 200);
command.Parameters.AddWithValue("@name", "banana");
int rowCount = await command.ExecuteNonQueryAsync();
Console.WriteLine(String.Format("Number of rows updated={0}", rowCount));
}
Console.WriteLine("Closing connection");
}
Console.WriteLine("Press RETURN to exit");
Console.ReadLine();
}
}
}
Use the following code to connect and delete the data by using a DELETE
SQL statement.
The code uses the MySqlConnection
class with method
Replace the Server
, Database
, UserID
, and Password
parameters with the values that you specified when you created the server and database.
using System;
using System.Threading.Tasks;
using MySqlConnector;
namespace AzureMySqlExample
{
class MySqlDelete
{
static async Task Main(string[] args)
{
var builder = new MySqlConnectionStringBuilder
{
Server = "YOUR-SERVER.mysql.database.azure.com",
Database = "YOUR-DATABASE",
UserID = "USER",
Password = "PASSWORD",
SslMode = MySqlSslMode.Required,
};
using (var conn = new MySqlConnection(builder.ConnectionString))
{
Console.WriteLine("Opening connection");
await conn.OpenAsync();
using (var command = conn.CreateCommand())
{
command.CommandText = "DELETE FROM inventory WHERE name = @name;";
command.Parameters.AddWithValue("@name", "orange");
int rowCount = await command.ExecuteNonQueryAsync();
Console.WriteLine(String.Format("Number of rows deleted={0}", rowCount));
}
Console.WriteLine("Closing connection");
}
Console.WriteLine("Press RETURN to exit");
Console.ReadLine();
}
}
}
To clean up all resources used during this quickstart, delete the resource group using the following command:
az group delete \
--name $AZ_RESOURCE_GROUP \
--yes
Events
Mar 31, 11 PM - Apr 2, 11 PM
The ultimate Microsoft Fabric, Power BI, SQL, and AI community-led event. March 31 to April 2, 2025.
Register today