In this article I will explain with an example, how to implement MySqlDataAdapter in ASP.Net Core (.Net Core) Razor Pages.
The records from the SQL Server Database Table will be fetched using MySqlDataAdapter.
Then the fetched records will be populated into a DataSet which will be used to populate the HTML Table in ASP.Net Core Razor Pages.
Though it is not a good practice to use DataSet or DataTable as Model in Razor Page architecture, thus user’s please make a note that this is just an informative article and does not recommend to use DataSet or DataTable as Model in ASP.Net Core Razor Pages.
Note: For beginners in ASP.Net Core (.Net Core 7) Razor Pages, please refer my article ASP.Net Core 7 Razor Pages: Hello World Tutorial with Sample Program example.
 
 

Database

I have made use of the following table Hobbies with the schema as follow.
ASP.Net Core Razor Pages: Implementing MySqlDataAdapter
 
I have already inserted few records in the table.
ASP.Net Core Razor Pages: Implementing MySqlDataAdapter
 
Note: You can download the database table SQL by clicking the download link below.
            Download SQL file
 
 

Namespaces

You will need to import the following namespaces.
using System.Data;
using MySql.Data.MySqlClient;
 
 

Razor PageModel (Code-Behind)

Inside the PageModel, the public property of DataSet class is created.
The PageModel consists of following Handler method.

Handler method for handling GET operation

Inside this Handler method, the records are fetched from the Customers Table using ADO.Net.
The records are inserted into a public property Customers (DataSet) using MySqlDataAdapter class object.
Finally, the public property of DataSet i.e. Customers is returned to the Razor Page.
public class IndexModel : PageModel
{
    public DataSet Customers { getset; }
 
    public void OnGet()
    {
        string constr = @"Data Source=localhost;port=3306;Initial Catalog=AjaxSamples;User Id=root;password=pass@123";
        string sql = "SELECT CustomerId, Name, Country FROM Customers";
        using (MySqlConnection con = new MySqlConnection(constr))
        {
            using (MySqlCommand cmd = new MySqlCommand(sql, con))
            {
                using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd))
                {
                    this.Customers = new DataSet();
                    sda.Fill(Customers);
                }
            }
        }
    }
}
 
 

Razor Page (HTML)

HTML Markup

Inside the Razor Page, the System.Data namespace is inherited.
For displaying the records, an HTML Table is used.
A FOR EACH loop will be executed over the rows of the DataTable which will generate the HTML Table rows with the Customer records.
@page
@using System.Data;
@model MySqlDataAdapter_Core_Razor.Pages.IndexModel
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th>Customer Id</th>
            <th>Name</th>
            <th>Country</th>
        </tr>
        @foreach (DataRow row in Model.Customers.Tables[0].Rows)
        {
            <tr>
                <td>@row["CustomerId"]</td>
                <td>@row["Name"]</td>
                <td>@row["Country"]</td>
            </tr>
        }
    </table>
</body>
</html>
 
 

Screenshot

ASP.Net Core Razor Pages: Implementing MySqlDataAdapter
 
 

Downloads