namespace MySqlConnectionMauiApp;
public partial class MainPage : ContentPage
private async void OnCheckConnectionClicked(object sender, EventArgs e)
ConnectionStatusLabel.Text = "Checking connection...";
ConnectionStatusLabel.TextColor = Colors.Gray;
var result = await Task.Run(() => CheckMySqlConnection());
ConnectionStatusLabel.Text = "Connection successful!";
ConnectionStatusLabel.TextColor = Colors.Green;
ConnectionStatusLabel.Text = $"Connection failed: {result.message}";
ConnectionStatusLabel.TextColor = Colors.Red;
private (bool isConnected, string message) CheckMySqlConnection()
string connectionString = "" +
using (var connection = new MySqlConnection(connectionString))
if (connection.State == System.Data.ConnectionState.Open)
return (true, "Connection successful!");
return (false, "Connection failed!");
return (false, ex.Message);