首页 > 解决方案 > Blazor 服务器 - 从 SQL 返回计数 - 返回 System.Collections.Generic.List`1[System.Object]

问题描述

我正在尝试从 SQL 中的存储过程返回行数。我已经为此工作了好几个小时了。我完全不知所措。

SQL 代码:在 SQL 中运行时返回 49 的结果。

begin
select Count (EmployeeID)

from dbo.Employees
Where (Employees.Active = 'True')


end

组件代码:

@attribute [Authorize(Roles = "Admin")]

@using System.Collections;
@using WahlenDataAccessLibrary;
@using WahlenDataAccessLibrary.Models;
@using BlazorApp1.Models;

@inject IEmployeesData employeeData;

<div class="widget">
    <h5 class="widget--title">Active Employees</h5>
    <div class="widget--body">
        <p class="widget--number">
            <span>@employeeCount</span>
            Active Employees
        </p>
    </div>

</div>




@code {

    private string employeeCount { get; set; }
    //private IEmployeeModel employeeCount = new EmployeeModel();

    protected override async Task OnInitializedAsync()
    {
        var count = await employeeData.EmployeeCount();

        //string employeeCount = await employeeData.EmployeeCount();

        string employeeCount = count.ToString();
        Console.WriteLine(employeeCount);
        if (employeeCount != null)
        {
            Console.WriteLine("generic value");
        }
        else
        {
            Console.WriteLine("no value");
        }
    }
}

DataFile 代码:从存储过程中获取值。

    public async Task<string> EmployeeCount()
    {

        var employeeCount = await _db.LoadValue("dbo.spWidget_EmployeeCount", "DefaultConnection");
        return employeeCount.ToString();
    }
}

使用“LoadValue”的数据文件。这链接回使用此代码的我的 SqlDataAccess 文件。

public async Task<string> LoadValue(string storedProcedure, string connectionStringName)
{
    string connectionString = _config.GetConnectionString(connectionStringName);

    using (IDbConnection connection = new SqlConnection(connectionString))
    {
        var data = await connection.QueryAsync(storedProcedure,
            commandType: CommandType.StoredProcedure);

        return data.ToString();
    }
}

当应用程序运行时,控制台会写入。

System.Collections.Generic.List`1[System.Object]
no value

标签: sqlcountblazorreturn-valueblazor-server-side

解决方案


我将在这里更新代码,因为我已经让它工作了。Task<IEnumerable> 更是如此这里的答案。如果您有一个简单地计算行数的 sql 查询,这就是您在前端的 Blazor 上访问该数据的方式。您可以将值作为字符串返回。

我的 sqlaccess 文件代码现在看起来像这样。

public async Task<IEnumerable<int>> LoadValue(string storedProcedure, string connectionStringName)
{
    string connectionString = _config.GetConnectionString(connectionStringName);

    using (IDbConnection connection = new SqlConnection(connectionString))
    {
        var data = await connection.QueryAsync<int>(storedProcedure,
            commandType: CommandType.StoredProcedure);

        return data;
    }
}

这是在前端组件上。

private string employeeCount { get; set; }
    
protected override async Task OnInitializedAsync()
{
employeeCount = await employeeData.EmployeeCount();


Console.WriteLine(employeeCount);
        if (employeeCount != null)
        {
            Console.WriteLine("generic value");
        }
        else
        {
            Console.WriteLine("no value");
    }

推荐阅读