sql - 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
解决方案
我将在这里更新代码,因为我已经让它工作了。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");
}
推荐阅读
- .net - 服务运行时无法更改服务名称
- node.js - Node.js 需要找不到模块的库
- html - 如何在 Angular 5 中以模态打开 url(网页视图)
- jvm - jProfiler。临时连接错误后无法正常工作
- jquery - 尽管格式良好的 Json(DataTables),但无法在我的表中添加行
- google-bigquery - 如何从具有 DATE 列的 BigQuery 表中导出 AVRO 文件并将其再次加载到 BigQuery
- javascript - 如何检查 WebSocket.server 连接是打开还是关闭?
- python - python tkinter自我错误
- php - laravel Guzzle 发布数据数组
- angularjs - Chartjs - 数据集颜色被 angular-chartjs 超载