.NET MAUI (Multi-platform App UI) is a cross-platform framework for building desktop and mobile applications with C# and XAML. With its help, developers can create applications running on Windows,
Mac, iOS, and Android operating systems, all from one codebase.
MySQL is one of the most popular databases for building web applications. It is also open-source, allowing everyone to establish a database instance and connect to it. Database connectivity
is essential because the application requires a reliable data source. Therefore, developers must ensure a quick and smooth connection to the database, as well as efficient data retrieval
and management within the application.
In this tutorial, we will explore the integration of MySQL with a MAUI application, allowing us to design and manage the database directly from the C# code.
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:
To connect to MySQL using the built-in Data Explorer, click Tools and select Connect to Database.
Choose MySQL as the data source, enter your server details and credentials, and click Connect.
Once connected, you can browse tables, execute queries, and manage data directly within Data Explorer.
Connect and retrieve MySQL data
In this foundational step, our goal is to establish a connection to the MySQL database and display data in a read-only list. This ensures our database connection is correctly
configured and provides the base, upon which we will build the other CRUD operations.
Data model (Actor.cs)
First, create a new C# class file named Actor.cs. This class will act as a model to hold the data for each actor record retrieved from the database.
Create an ActorService.cs file. We will start by adding a single method, GetAllActors, which will be responsible for querying the database and returning a list of all actors.
// MySqlMAUI/ActorService.csusing Devart.Data.MySql;
using System.Collections.Generic;
namespaceMySqlMAUI;
publicclassActorService
{
publicstatic List<Actor> GetAllActors()
{
// ... (code as provided in the previous turn)
}
}
User interface (MainPage.xaml)
Modify MainPage.xaml to contain a ListView for displaying the actors and a Label for status messages. At this stage, there are no input fields or buttons.
<?xml version="1.0" encoding="utf-8" ?><ContentPagexmlns="http://schemas.microsoft.com/dotnet/2021/maui"xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"x:Class="MySqlMAUI.MainPage"Title="Actor Management"><GridRowDefinitions="*,Auto"Padding="20"RowSpacing="15"><!-- Actor List --><ListViewx:Name="ActorListView"Grid.Row="0"><ListView.ItemTemplate><DataTemplate><ViewCell><HorizontalStackLayoutPadding="10"><LabelText="{Binding ActorId, StringFormat='ID: {0}'}"WidthRequest="60" /><LabelText="{Binding FirstName}"WidthRequest="100" /><LabelText="{Binding LastName}"WidthRequest="100" /><LabelText="{Binding LastUpdate, StringFormat='{0:yyyy-MM-dd HH:mm}'}"HorizontalOptions="EndAndExpand" /></HorizontalStackLayout></ViewCell></DataTemplate></ListView.ItemTemplate></ListView><!-- Status Bar --><Labelx:Name="StatusLabel"Grid.Row="1"Text="Connecting..."HorizontalOptions="Center" /></Grid></ContentPage>
UI Logic (MainPage.xaml.cs)
Update the code-behind file MainPage.xaml.cs. The code will call the LoadActors method from the constructor, ensuring the data is fetched immediately upon application launch.
You have now built a functional, read-only .NET MAUI application that connects to a MySQL database and displays the data. This confirms that your core setup is working correctly.
Insert MySQL data
We will now enhance the application by adding the ability to create new records. This involves adding input fields and a button to the UI and implementing the logic to execute an INSERT SQL statement.
Update the UI (MainPage.xaml)
Modify MainPage.xaml to add Entry fields for the first and last name, and an Insert button. The main Grid layout is adjusted to accommodate these new elements.
<!-- MySqlMAUI/MainPage.xaml --><!-- ... (XML header) ... --><GridRowDefinitions="Auto,*,Auto"Padding="20"RowSpacing="15"><!-- Input Form --><GridGrid.Row="0"ColumnDefinitions="*,*"ColumnSpacing="10"RowDefinitions="Auto,Auto"><Entryx:Name="FirstNameEntry"Placeholder="First Name"Grid.Row="0"Grid.Column="0" /><Entryx:Name="LastNameEntry"Placeholder="Last Name"Grid.Row="0"Grid.Column="1" /><ButtonText="Insert"Clicked="OnInsertClicked"Grid.Row="1"Grid.ColumnSpan="2" /></Grid><!-- ... (Rest of the XAML remains the same) ... --></Grid><!-- ... (XML footer) ... -->
Update the data service (ActorService.cs)
Add the InsertActor method to your ActorService.cs file. This method takes the first and last names, connects to the database, and executes a parameterized INSERT query.
// MySqlMAUI/ActorService.cs// ... (add this method inside the ActorService class)publicstaticvoidInsertActor(string firstName, string lastName)
{
// ... (code as provided in the previous turn)
}
Update the UI logic (MainPage.xaml.cs)
Modify MainPage.xaml.cs to handle the new functionality. Add a private field _selectedActor (which we'll use more in the next steps) and create the OnInsertClicked and ClearForm methods.
// MySqlMAUI/MainPage.xaml.cs// ... (add this field at the top of the MainPage class)private Actor? _selectedActor;
// ... (add these methods inside the MainPage class)privateasyncvoidOnInsertClicked(object? sender, EventArgs e)
{
// ... (code as provided in the previous turn)
}
privatevoidClearForm()
{
// ... (code as provided in the previous turn)
}
The application can now add new records to the database. Users can enter data, press the Insert button, and the list updates immediately to reflect the new entry.
Update MySQL data
In this step, we will implement the ability to edit existing records. This requires logic to select an item from the list, populate the input fields with its data,
and then save the changes with an UPDATE SQL command.
Update the UI (MainPage.xaml)
In MainPage.xaml, add an Update button next to the Insert button and attach an ItemSelected event handler to the ListView.
<!-- MySqlMAUI/MainPage.xaml --><!-- ... (inside the input form Grid, update the button layout) ... --><GridGrid.Row="1"Grid.ColumnSpan="2"ColumnDefinitions="*,*"><ButtonText="Insert"Clicked="OnInsertClicked"Grid.Column="0" /><ButtonText="Update"Clicked="OnUpdateClicked"Grid.Column="1" /></Grid><!-- ... (in the ListView tag) ... --><ListViewx:Name="ActorListView"Grid.Row="1"ItemSelected="OnActorSelected">
Update the data service (ActorService.cs)
Add the UpdateActor method to ActorService.cs. This method executes a parameterized UPDATE query using the actor's ID.
// ... (add this method inside the ActorService class)publicstaticvoidUpdateActor(int actorId, string firstName, string lastName)
{
// ... (code as provided in the previous turn)
}
Update the UI logic (MainPage.xaml.cs)
Add the OnActorSelected and OnUpdateClicked methods to MainPage.xaml.cs. OnActorSelected will populate the form when a user taps an item in the list.
OnUpdateClicked will save the changes.
// ... (add these methods inside the MainPage class)privatevoidOnActorSelected(object? sender, SelectedItemChangedEventArgs e)
{
// ... (code as provided in the previous turn)
}
privateasyncvoidOnUpdateClicked(object? sender, EventArgs e)
{
// ... (code as provided in the previous turn)
}
The application now supports editing data. Users can select an actor, change their details in the input form, and save the changes back to the database.
Delete MySQL data
In the final step, we will implement the delete functionality. This involves adding a Delete button and the logic to execute a DELETE SQL command, including a confirmation step
to prevent accidental data loss.
Update the UI (MainPage.xaml)
Modify MainPage.xaml to include a Delete button, completing our CRUD controls set.
// MySqlMAUI/ActorService.cs// ... (add this method inside the ActorService class)publicstaticvoidDeleteActor(int actorId)
{
// ... (code as provided in the previous turn)
}
Update the UI logic (MainPage.xaml.cs)
Finally, add the OnDeleteClicked method to MainPage.xaml.cs. This method first confirms the user's action with a pop-up dialog before proceeding with deletion.
// MySqlMAUI/MainPage.xaml.cs// ... (add this method inside the MainPage class)privateasyncvoidOnDeleteClicked(object? sender, EventArgs e)
{
// ... (code as provided in the previous turn)
}
You have now completed the application. It has full CRUD functionality, allowing users to read, create, update, and delete records from the MySQL database in a user-friendly way.
Video tutorial: How to connect a .NET MAUI application to a MySQL database
Conclusion
This tutorial describes how to connect a .NET MAUI application to a MySQL database using dotConnect for MySQL and demonstrates how to manage the database. We explored how to create tables, insert data, read from the tables, and update or delete records.
Using the dotConnect for MySQL data provider greatly simplifies tasks by supporting database-specific features and integrating seamlessly with Visual Studio. This allows you to leverage your preferred IDE for application development with enhanced functionality.
Download a free trial to test the solution's functionality under real-world workloads and evaluate its efficiency within your workflows.
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 the 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.