Link to home
Create AccountLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ASP.net Razor page that pulls data from a SQL database

Hi


In an ASP.net Blazor project I want to add a new razor page that pulls data from an SQL table using a connection string. How do I alter the bottom part of the following code to rather pull data from an MS SQL Server database database? Also what type of page should I choose based on the image further down.

@page "/fetchdata"
@using Blazor1.Shared @inject HttpClient Http <h1>Weather forecast</h1> <p>This component demonstrates fetching data from the server.</p> @if (forecasts == null) {     <p><em>Loading...</em></p> } else {     <table class="table">         <thead>             <tr>                 <th>Date</th>                 <th>Temp. (C)</th>                 <th>Temp. (F)</th>                 <th>Summary</th>             </tr>         </thead>         <tbody>             @foreach (var forecast in forecasts)             {                 <tr>                     <td>@forecast.Date.ToShortDateString()</td>                     <td>@forecast.TemperatureC</td>                     <td>@forecast.TemperatureF</td>                     <td>@forecast.Summary</td>                 </tr>             }         </tbody>     </table> } @code {     private WeatherForecast[] forecasts;     protected override async Task OnInitializedAsync()     {         forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast");     } }

Open in new window


User generated image



ASKER CERTIFIED SOLUTION
Avatar of Misha
Misha
Flag of Russian Federation image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer