In this article I will explain with an example, how to use MySqlDataReader in ASP.Net Core MVC.
The records from the Database table will be read using MySqlDataReader and then copied into the Generic List collection of class objects, which is later used for populating HTML Table in ASP.Net Core MVC.
Note: For beginners in ASP.Net Core (.Net Core 7) and MySQL, please refer my article ASP.Net Core: Using MySql Database with MySql Connector Tutorial with example.
 
 

Database

I have made use of the following table Customers with the schema as follows.
Using MySqlDataReader in ASP.Net Core MVC
 
I have already inserted few records in the table.
Using MySqlDataReader in ASP.Net Core MVC
 
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 MySql.Data.MySqlClient;
 
 

Model

The Model class consists of the following three properties.
public class CustomerModel
{
    public int CustomerId { getset; }
    public string Name { getset; }
    public string Country { getset; }
}
 
 

Controller

The Controller consists of the following Action method.
Action method for handling GET operation
Inside this Action method, first the connection is read from the ConnectionStrings section of the AppSettings.json file.
Note: For more details on how to read Connection String from AppSettings.json, please refer my article .Net Core 7: Read Connection String from AppSettings.json file.
 
Then, a Generic List collection of CustomerModel class object is created.
The records are fetched from the Customers Table using MySqlDataReader and then using WHILE loop, the records are copied into the Generic List collection of CustomerModel class objects.
Finally, the Generic List collection of CustomerModel class object is returned to the View.
public class HomeController : Controller
{
    public IConfiguration Configuration { get; set; }
 
    public HomeController(IConfiguration _configuration)
    {
        this.Configuration = _configuration;
    }
 
    public IActionResult Index()
    {
        List<CustomerModel> customers = new List<CustomerModel>();
        string constr = this.Configuration.GetSection("ConnectionStrings")["MyConn"];
        string sql = "SELECT CustomerId, Name, Country FROM Customers";
        using (MySqlConnection con = new MySqlConnection(constr))
        {
            using (MySqlCommand cmd = new MySqlCommand(sql, con))
            {
                con.Open();
                using (MySqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        customers.Add(new CustomerModel
                        {
                            CustomerId = int.Parse(sdr["CustomerId"].ToString()),
                            Name = sdr["Name"].ToString(),
                            Country = sdr["Country"].ToString()
                        });
                    }
                }
                con.Close();
            }
        }
        return View(customers);
    }
}
 
 

View

HTML Markup

Inside the View, in the very first line the CustomerModel class is declared as IEnumerable which specifies that it will be available as a Collection.
For displaying the records, an HTML Table is used. A FOR EACH loop will be executed over the Model which will generate the HTML Table rows with the Customer records.
@using MySqlDataReader_Core.Models;
@model IEnumerable<CustomerModel>
@{
    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>CustomerId</th>
            <th>Name</th>
            <th>Country</th>
        </tr>
        @foreach (CustomerModel customer in Model)
        {
            <tr>
                <td>@customer.CustomerId</td>
                <td>@customer.Name</td>
                <td>@customer.Country</td>
            </tr>
        }
    </table>
</body>
</html>
 
 

Screenshot

Using MySqlDataReader in ASP.Net Core MVC
 
 

Downloads