感谢您的观察,我确实将它们应用了以下代码,但在测试api之后未返回任何数据。我什至用我的连接字符串代替dbcontext还是一无所有!当阅读器关闭且阅读器处于打开状态时,我收到此错误无效的调用FieldCount的尝试。我在这种情况下已有一周的时间了,任何帮助。FromSql查询仅返回单个表的实体。我的存储过程带有一个参数,并具有4个表的联接。我已经尝试使用“ ExecuteSqlCommand”查询,因为结果是-1。
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ActivaMobileAgent.Persistence;
using Microsoft.EntityFrameworkCore;
using ActivaMobileAgent.Model;
using System.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;
using System;
using Dapper;
using System.Data;
using System.Data.Common;
namespace ActivaMobileAgent.Controllers
{
[Route("/api/Policy")]
public class PolicyController : Controller
{
private readonly ActivaMobileAgentDbContext context;
public PolicyController(ActivaMobileAgentDbContext context) => this.context = context;
public readonly string connectionString = "Data Source=Localhost;Initial Catalog=HELLOxx;User=sa; Password=Helloxx";
[HttpGet("{id}")]
public async Task<IActionResult> GetPolicy(string id)
{
using (var connection = new SqlConnection("Data Source=Localhost;Initial Catalog=Helios;User=sa; Password=P@ssw0rd"))
{
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandText ="dbo.sproc_Contract_Get";
command.Parameters.Add(new SqlParameter("@ContractNumber", SqlDbType.VarChar) { Value = id });
connection.Open();
using (var result = await command.ExecuteReaderAsync())
{
return Json(result);
}
}
}
}
}
相关分类