In this article I will explain with an example, how to implement MySqlDataAdapter in ASP.Net Core MVC.
The records from the 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 MVC.
Though it is not a good practice to use DataSet or DataTable as Model in View in MVC 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 View in ASP.Net Core MVC.
Note: For beginners in ASP.Net Core MVC, please refer my article ASP.Net MVC Core Hello World Tutorial with Sample Program example.
 
 
Database
I have made use of the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.
ASP.Net Core: Implementing MySqlDataAdapter
 
I have already inserted few records in the table.
ASP.Net Core: 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;
 
 
Controller
The Controller consists of the following Action method.
Action method for handling GET operation
Inside this Action method, the records are fetched from the Customers Table using ADO.Net.
The records are inserted into a DataSet using MySqlDataAdapter class object.
Finally, the DataSet is returned to the View.
public class HomeController : Controller
{
    public IActionResult Index()
    {
        DataSet ds = new DataSet();
        string conString = "Data Source=localhost;port=3306;Initial Catalog=AjaxSamples;User Id=root;password=pass@123";
        using (MySqlConnection con = new MySqlConnection(conString))
        {
            string query = "SELECT * FROM Customers";
            using (MySqlCommand cmd = new MySqlCommand(query))
            {
                cmd.Connection = con;
                using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd))
                {
                    sda.Fill(ds);
                }
            }
        }
        return View(ds);
    }
}
 
 
View
Inside the View, DataSet class is declared as Model for the View.
For displaying the records, an HTML Table is used. A loop will be executed over the rows of the DataTable which will generate the HTML Table rows with the Customer records.
@using System.Data
@model DataSet
@{
    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 (DataRow row in Model.Tables[0].Rows)
        {
            <tr>
                <td>@row["CustomerId"]</td>
                <td>@row["Name"]</td>
                <td>@row["Country"]</td>
            </tr>
        }
    </table>
</body>
</html>
 
 
Screenshot
ASP.Net Core: Implementing MySqlDataAdapter
 
 
Downloads