【发布时间】:2019-03-01 02:25:45
【问题描述】:
我已经使用 Angular 创建了一个新的 ASP.NET Core 2.1 Web 应用程序
Visual Studio 创建一个使用 Entity Framework Core 和 ASP.NET Core MVC 的项目。
我有这个问题:我必须从存储过程中读取记录:
CREATE PROCEDURE GetEmployees
(@PageIndex INT,
@PageSize INT)
AS
BEGIN
SELECT *
FROM employee
ORDER BY id
OFFSET @PageSize * (@PageIndex - 1) ROWS
FETCH NEXT @PageSize ROWS ONLY;
SELECT COUNT(*) AS totalCount
FROM employee;
END
我找到了a question that almost solves my question,但不幸的是还是有一些东西不起作用。
这是我的代码:
namespace Angular.Controllers
{
//[Produces("application/json")]
[Route("api/Employees")]
public class EmployeesController : Controller
{
private readonly ApplicationDbContext _context;
public EmployeesController(ApplicationDbContext context)
{
_context = context;
}
// GET: api/Employees/pageIndex/1/pageSize/1
[HttpGet("pageIndex/{pageIndex}/pageSize/{pageSize}")]
public Task<IActionResult> GetEmployeeP([FromRoute] int pageIndex, int pageSize)
{
SqlParameter pageIndexParam = new SqlParameter("@PageIndex", SqlDbType.Int);
pageIndexParam.Value = pageIndex;
SqlParameter pageSizeParam = new SqlParameter("@pageSize", SqlDbType.Int);
pageSizeParam.Value = pageSize;
// SqlParameter pageIndexParam = new SqlParameter("@PageIndex", pageIndex);
// SqlParameter pageSizeParam = new SqlParameter("@pageSize", pageSize);
var cmd = _context.Database.GetDbConnection().CreateCommand();
cmd.CommandText = "GetEmployees"; // The name of the stored procedure
cmd.CommandType = System.Data.CommandType.StoredProcedure;
// the 2 parameters to be passed to the procedure
var param = cmd.CreateParameter();
param.ParameterName = "@PageIndex";
param.Value = pageIndexParam;
cmd.Parameters.Add(param);
var param2 = cmd.CreateParameter();
param2.ParameterName = "@pageSize";
param2.Value = pageSizeParam;
cmd.Parameters.Add(param2);
try
{
// connection.Open();
// _context.Database.GetDbConnection().Open(); // it crashes after this line
_context.Database.OpenConnection(); // it crashes after this line
var dr = cmd.ExecuteReader(); // ArgumentException: No mapping exists from object type System.Data.SqlClient.SqlParameter to a known managed provider native type.
List<Employee> listEmp = new List<Employee>();
while (dr.Read())
{
// Is there a faster way to read the whole record?
// Or should I write a line for each field?
Employee emp = new Employee();
emp.ID = System.Int32.Parse(dr["id"].ToString());
emp.Fname = dr["FName"].ToString();
emp.email = dr["email"].ToString();
emp.Lname = dr["LName"].ToString();
listEmp.Add(emp);
dr.NextResult();
}
return Ok(listEmp);
}
catch (Exception ex)
{
// how can I return an error in json format?
throw;
}
}
}
}
问题出在脚本ExecuteReader所在的那一行:
ArgumentException:不存在从对象类型 System.Data.SqlClient.SqlParameter 到已知托管提供程序本机类型的映射。
我使用 Microsoft SQL Server 在 Startup.cs 文件中,我是这样配置连接的:
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer("Server=localhost\\SQLEXPRESS;Database=master;Trusted_Connection=True;"));
你能帮帮我吗?
也许我设法正确传递了参数(请参阅我的解决方案,在答案中)但我无法提取记录
【问题讨论】:
-
你得到什么错误?也许连接已经打开?
-
ArgumentException:不存在从对象类型 System.Data.SqlClient.SqlParameter 到已知托管提供程序本机类型的映射。
-
创建
SqlParameter对象时,请尝试指定数据类型并稍后设置值。例如:var pageIndexParam = new SqlParameter("@PageIndex", SqlDbType.Int); pageIndexParam.Value = pageIndex; -
没有。它甚至不能那样工作
-
同样的错误?不同的错误?你需要学会停止说“它不起作用”并提供更多细节,否则没有人可以帮助你。
标签: sql-server asp.net-mvc-4 c#-4.0 entity-framework-core