In this article I will explain with an example, how to insert data into
MySQL database using
Dapper library in ASP.Net Core MVC.
Installing Dapper package using Nuget
Database
I have made use of the following table Customers with the schema as follows.
Note: You can download the database table SQL by clicking the download link below.
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 Dapper;
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
Inside this Action method, first the connection is read from the ConnectionStrings section of the AppSettings.json file.
Then, the SQL query and
CustomerModel class object is passed to the
ExecuteScalar method of the
Dapper library which then inserts the record in the
Customers Table.
Finally, the CustomerId of the inserted record is set and 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(CustomerModel customer)
{
string sql = "INSERT INTO Customers (Name, Country) VALUES(@Name, @Country);";
sql += " SELECT LAST_INSERT_ID();";
string constr = this.Configuration.GetSection("ConnectionStrings")["MyConn"];
using (MySqlConnection con = new MySqlConnection(constr))
{
customer.CustomerId = Convert.ToInt32(con.ExecuteScalar(sql, customer));
}
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 Table, which consists of an HTML INPUT TextBox and HTML SELECT (DropDownList) and a Submit Button.
Inside the View, the following script file is inherited:
1. jquery.min.js
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
CustomerId is displayed using
JavaScript Alert Message Box.
@model Dapper_Insert_MySQL_Core_MVC.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">
<table cellpadding="0" cellspacing="0">
<tr>
<td>Name: </td>
<td>
<input type="text" asp-for="Name" />
</td>
</tr>
<tr>
<td>Country: </td>
<td>
<select asp-for="Country">
<option value="Please select">Please select</option>
<option value="United States">United States</option>
<option value="India">India</option>
<option value="France">France</option>
<option value="Russia">Russia</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
@if (Model != null)
{
<script type="text/javascript">
$(function () {
alert("Inserted Customer ID: " + @Model.CustomerId);
});
</script>
}
</body>
</html>
Screenshots
The Form
Record after Insert in database
Downloads