简体   繁体   English

Blazor 创建服务,从 SQL 服务器存储过程中提取数据

[英]Blazor creating a service, to pull data from SQL Server stored procedure

I'm a noob when it comes to Blazor, and C# for that matter.对于 Blazor 和 C#,我是个菜鸟。

I'm trying to create a simple service.我正在尝试创建一个简单的服务。 Simple process that includes running a stored procedure, and displaying its results in a table.包括运行存储过程并在表中显示其结果的简单过程。 I'm starting simple, by just making sure that the process runs.我从简单开始,只需确保进程运行即可。

Here's what my service looks like:这是我的服务的样子:

public class ACheckerService : IACheckerService
{
    // Database connection
    private readonly SqlConnectionConfig _config;

    public ACheckerService(SqlConnectionConfig config)
    {
        _config = config;
    }

    public async Task<IEnumerable<AChecker>> GetAllExceptions()
    {
        IEnumerable<AChecker> exceptions;

        using (var conn = new SqlConnection(_config.Value))
        {
            exceptions = await conn.QueryAsync<AChecker>("Auto.GetAll", commandType: System.Data.CommandType.StoredProcedure);
        }

        return exceptions.ToList(); ;
    }
}

I created AChecker model class which comprises of the fields that are to be pulled in by the stored procedure.我创建了AChecker model class,其中包含要由存储过程引入的字段。 Just 4 fields.只有 4 个字段。

Once I had this done, I added a Blazor component ( .Razor )完成此操作后,我添加了一个 Blazor 组件 ( .Razor )

This is what my code looks like:这是我的代码的样子:

@page "/Data/Auto"

@using AChecker.Data

@inject IACheckerService CheckerService

<h3>Auto</h3>

<p>This component demonstrates Auto Exceptions</p>

@if (aChecker is null)
{
    <p><em>Loading....</em></p>
}
else
{
    <p><em>Data....</em></p>
}

@code {
    private IEnumerable<AChecker> aChecker;

    protected override async Task OnInitializedAsync()
    {
        aChecker = await CheckerService.GetAllAutoExceptions();
    }
}

I added this service in Startup.Cs like:我在 Startup.Cs 中添加了此服务,例如:

services.AddSingleton<ACheckerService>();

Is there any issue with the code above or why it would not work?上面的代码是否有任何问题或为什么它不起作用?

I have this service added in my app, basically when I click on the menu, I expect to see either Loading or Data (ideally down the road, I want to create a table and populate the fields from the stored procedure in it, there are 4 fields), however I get a bunch of errors:我在我的应用程序中添加了这项服务,基本上当我点击菜单时,我希望看到加载或数据(理想情况下,我想创建一个表并从其中的存储过程填充字段,有4 个字段),但是我得到了一堆错误:

InvalidOperationException: Cannot provide a value for property CheckerService on type AChecker.Pages.MyPages.Auto. InvalidOperationException:无法为 AChecker.Pages.MyPages.Auto 类型的属性 CheckerService 提供值。 There is no registered service of type AChecker.Data.IACheckerService没有 AChecker.Data.IACheckerService 类型的注册服务

Try registering the service using this overload instead which specifies the interface and its concrete implementation:尝试使用此重载注册服务,而不是指定接口及其具体实现:

services.AddSingleton<IACheckerService, ACheckerService>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建SQL CLR存储过程以从Web服务获取数据并将结果插入到SQL Server表中 - How to create a SQL CLR stored procedure to get data from a web service and insert results into a SQL Server table 无法从 Blazor 应用程序中的 Register.cshtml.cs 执行 SQL Server 存储过程 - Unable to execute a SQL Server stored procedure from Register.cshtml.cs in Blazor app SQL 在 Blazor 服务器中加入存储过程不返回列表 - SQL Join Stored Procedure Does Not Return List In Blazor Server 在SQL Server中创建存储过程和视图的问题 - problem with creating stored procedure and view in sql server 分析从SQL Server存储过程返回的数据 - Analyze data returned from SQL Server stored procedure 无法使用存储过程从 SQL Server 检索数据 - Cannot retrieve data from SQL Server using Stored Procedure 如何从SQL Server存储过程中读取数据表 - How to read data table from SQL Server stored procedure 使用存储过程从SQL Server返回数据 - Return data from SQL Server using Stored Procedure 使用存储过程从 SQL 服务器插入和获取数据 - Insert and Get Data from SQL Server using Stored Procedure SqlDataReader不从SQL Server存储过程返回任何数据 - SqlDataReader not returning any data from SQL Server stored procedure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM