In this article I will explain with an example, how to display data from Database using ADO.Net 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.
I have made use of the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.
ASP.Net Core MVC: Display data from Database using ADO.Net
I have already inserted few records in the table.
ASP.Net Core MVC: Display data from Database using ADO.Net
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
You will need to import the following namespaces.
using System.Data;
using System.Data.SqlClient;
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 SqlDataAdapter class object.
Finally, the DataSet is returned to the View.
public class HomeController : Controller
    public IActionResult Index()
        DataSet ds = new DataSet();
        string constr = @"Data Source=.\SQL2019;Initial Catalog=AjaxSamples;Integrated Security=true";
        using (SqlConnection con = new SqlConnection(constr))
            string query = "SELECT * FROM Customers";
            using (SqlCommand cmd = new SqlCommand(query))
                cmd.Connection = con;
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
        return View(ds);
Inside the View, the 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>
    <meta name="viewport" content="width=device-width" />
    <table cellpadding="0" cellspacing="0">
        @foreach (DataRow row in Model.Tables[0].Rows)
ASP.Net Core MVC: Display data from Database using ADO.Net