In this article I will explain with an example, how to use ADO.Net in ASP.Net Core (.Net Core 8) Razor Pages.
Note: For beginners in ASP.Net Core (.Net Core 8) Razor Pages, please refer my article ASP.Net Core 8 Razor Pages: Hello World Tutorial with Sample Program example.
 
 

Installing System.Data.SqlClient package

By default, like ASP.Net, ASP.Net MVC and .Net Core 3.0 Razor Pages, System.Data.SqlClient package is not available in ASP.Net Core (.Net Core 8) Razor Pages.
Thus, in order to use System.Data.SqlClient package, you need to install the System.Data.SqlClient package from Nuget using Package Manager Console.
Note: For more details on how to install System.Data.SqlClient package in ASP.Net Core (.Net Core 8) Razor Pages, please refer my Using System.Data.SqlClient in .Net Core 8.0 Razor Pages.
 
 

Database

I have made use of the following table Customers with the schema as follows.
Using ADO.Net in .Net Core 8.0 Razor Pages
 
I have already inserted few records in the table.
Using ADO.Net in .Net Core 8.0 Razor Pages
 
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 System.Data.SqlClient;
 
 

Index PageModel (Code-Behind)

The PageModel consists of following Handler method.

Handler method for handling GET operation

Inside this Handler method, the records are fetched from the Customers Table using ADO.Net.
The records are inserted into a public property Customers (DataSet) using SqlDataAdapter class object.
Finally, the Customers is returned to the Razor Page.
public class IndexModel : PageModel
{
    public DataSet Customers { get; set; }
 
    public void OnGet()
    {
        string constr = @"Data Source=.\SQL2019;Initial Catalog= AjaxSamples;UIDsa;pwd=pass@123";
        using (SqlConnection con = new SqlConnection(constr))
        {
            string query = "SELECT * FROM Customers";
            using (SqlCommand cmd = new SqlCommand(query, con))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                     Customers = new DataSet();
                    sda.Fill(Customers);
                }
            }
        }
    }
}
 
 

Razor Page (HTML)

HTML Markup

Inside the Razor Page, the System.Data namespace is inherited.
For displaying the records, an HTML Table is used. A FOR EACH loop will be executed over the rows of the DataTable which will generate the HTML Table rows with the Customer records.
@page
@using System.Data;
@model SqlClient_Core_Razor.Pages.IndexModel
 
@{
     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.Customers.Tables[0].Rows)
        {
            <tr>
                <td>@row["CustomerId"]</td>
                <td>@row["Name"]</td>
                <td>@row["Country"]</td>
            </tr>
        }
    </table>
</body>
</html>
 
 

Screenshot

Using ADO.Net in .Net Core 8.0 Razor Pages
 
 

Downloads