代码之家  ›  专栏  ›  技术社区  ›  Koosh

Blazor正在创建服务,以从sqlserver存储过程中提取数据

  •  0
  • Koosh  · 技术社区  · 4 年前

    说到布拉佐,我是个无名小卒,在这件事上我是个傻瓜。

    我正在尝试创建一个简单的服务。包含运行存储过程并在表中显示其结果的简单过程。我开始很简单,只要确保进程运行。

    我的服务是这样的:

    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(); ;
        }
    }
    

    我创造了 AChecker

    .Razor )

    我的代码是这样的:

    @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();
        }
    }
    

    services.AddSingleton<ACheckerService>();
    

    上面的代码有什么问题吗?或者为什么它不能工作?

    我在我的应用程序中添加了这项服务,基本上,当我点击菜单时,我希望看到加载或数据(理想情况下,我希望创建一个表并从其中的存储过程填充字段,共有4个字段),但是我得到了一系列错误:

    1 回复  |  直到 4 年前
        1
  •  2
  •   Noah Stahl    4 年前

    请尝试使用此重载注册服务,该重载指定接口及其具体实现:

    services.AddSingleton<IACheckerService, ACheckerService>();