In this article I will explain with an example, how to call
MySQL Stored Procedure with Parameters in ASP.Net Core MVC.
The article explains with a simple example where a
Stored Procedure will be passed with a parameter and it returns the matching records. In similar way one can pass multiple parameters.
Download and Install the MySQL Connector
You will need to download and install the
MySQL Connector in order to connect to the
MySQL database in ASP.Net Core.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Stored Procedure
The following
Stored Procedure accepts a parameter
custId Integer parameter and is matched with the
CustomerId field of the
Customers Table of
MySQL database.
DELIMETER //
CREATE PROCEDURE Customers_GetCustomer(IN custId INT)
BEGIN
SELECT Name
,Country
FROM Customers
WHERE CustomerId = custId;
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;
Controllers
The Controller consists of following Action methods.
Action Method for handling GET operation
Inside this Action method, simply the view is returned.
Action Method for handling POST operation
This Action method accepts TextBox value i.e. CustomerId as parameter.
An object of CustomerModel class is created and the connection is read from the ConnectionStrings section of the AppSettings.json file.
An object of
MySqlCommand class is created and
CustomerId is added parameter and using
ExecuteReader the records are fetched from the
MySQL database.
Finally, the 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()
{
return View();
}
[HttpPost]
public IActionResult Index(int customerId)
{
CustomerModel customer = new CustomerModel();
string constr = this.Configuration.GetSection("ConnectionStrings")["MyConn"];
string spName = "Customers_GetCustomerById";
using (MySqlConnection con = new MySqlConnection(constr))
{
using (MySqlCommand cmd = new MySqlCommand(spName, con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@custId", customerId);
con.Open();
using (MySqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customer = (new CustomerModel
{
Name = sdr["Name"].ToString(),
Country = sdr["Country"].ToString()
});
}
}
con.Close();
}
}
return View(customer);
}
}
View
HTML Markup
Inside the View, in the very first line the CustomerModel class is declared as Model for the View.
The View consists of an HTML Form which has been created using the following TagHelpers attributes.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The View also consists of an HTML TextBox created and a Submit button.
Submitting the Form
When the Submit button is clicked then, the CustomerModel class object is checked for NULL and if it is not NULL then the fetched records are displayed in HTML Table.
@model MySQL_Call_SP_Core.Models.CustomerModel
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post" asp-controller="Home" asp-action="Index">
Search:
<input type="text" name="customerId" />
<input type="submit" value="Search" />
<hr />
@if (Model != null)
{
<table cellpadding="0" cellspacing="0">
<tr>
<th>Name</th>
<th>Country</th>
</tr>
<tr>
<td>@Model.Name</td>
<td>@Model.Country</td>
</tr>
</table>
}
</form>
</body>
</html>
Screenshot
Demo
Downloads