In this article I will explain with an example, how to select data from MySQL database with Stored Procedure in ASP.Net Core MVC.
Note: For beginners in ASP.Net Core 7, please refer my article ASP.Net Core 7: Hello World Tutorial with Sample Program example.
 
 

Database

I have made use of the following table Customers with the schema as follows.
Select data from MySQL Database with Stored Procedure in ASP.Net Core
 
I have already inserted few records in the table.
Select data from MySQL Database with Stored Procedure in ASP.Net Core
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 

Stored Procedure

The following Stored Procedure is used to display the records from MySQL database.
DELIMITER //
CREATE PROCEDURE Customers_GetCustomers()
BEGIN
    SELECT CustomerId, Name, Country
    FROM Customers
END //
DELIMITER ;
 
 

Model

The Model class consists of following properties.
public class CustomerModel
{
    public int CustomerId { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
}
 
 

Namespaces

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

Controller

The Controller consists of 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.
 
The Generic List collection of CustomerModel class object is created and the connection to the database is established using the MySqlConnection class.
Then, the records are fetched from the Customers Table of MySQL database with Stored Procedure using ExecuteReader method of MySqlCommand class.
Note: For more details on how to use ExecuteReader method, please refer my article Using MySqlCommand ExecuteReader Example in ASP.Net with C# and VB.Net.
 
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()
    {
        string spName = "Customers_GetCustomers";
        string constr = this.Configuration.GetSection("ConnectionStrings")["MyConn"];
 
        List<CustomerModel> customers = new List<CustomerModel>();
        using (MySqlConnection con = new MySqlConnection(constr))
        {
            using (MySqlCommand cmd = new MySqlCommand(spName, con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                con.Open();
                using (MySqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        customers.Add(new CustomerModel
                        {
                            CustomerId = Convert.ToInt32(sdr["CustomerId"]),
                            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 Generic List collection of CustomerModel class is declared as Model for the View.
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 Select_SP_MySQL_Core.Models
@modelList<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>Customer Id</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

Select data from MySQL Database with Stored Procedure in ASP.Net Core
 
 

Downloads