谁能指导我如何在 ASP.NET MVC / EF
应用程序中执行 SQL Server 存储过程并获取结果?
SQL Server 存储过程
CREATE PROCEDURE dbo.StoredProcedure2 AS
declare @parameter2 int
SET @parameter2 = 4
RETURN @parameter2
MVC 代码
private readonly TestDatastoreContext _context = new TestDatastoreContext();
public ViewResult Index(string id)
{
ViewData["EnvironmentId"] = id;
using (_context)
{
_context.Database.Connection.Open();
var command = _context.Database.Connection.CreateCommand();
command.CommandText = "dbo.StoredProcedure2";
command.CommandType = System.Data.CommandType.StoredProcedure;
var test = (command.ExecuteScalar());
}
var bigView = new BigViewModel
{
VersionsModel = _context.Versions.ToList(),
EnvironmentViewModel = _context.Environments.ToList(),
};
return View(model: bigView);
}