use*_*405 9 c# linq asp.net-mvc entity-framework sql-server-2008
任何人都可以指导我如何在ASP.NET MVC/EF
应用程序中执行SQL Server存储过程并获得结果吗?
SQL Server存储过程
CREATE PROCEDURE dbo.StoredProcedure2 AS
declare @parameter2 int
SET @parameter2 = 4
RETURN @parameter2
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
您的问题是:您从存储过程返回值(使用RETURN @paramter2),但您的.NET代码正在尝试读取结果集; 通过SELECT .....在存储过程中使用语句来"返回"的东西
因此,将存储过程更改为:
CREATE PROCEDURE dbo.StoredProcedure2 AS
declare @parameter2 int
SET @parameter2 = 4
SELECT @parameter2
Run Code Online (Sandbox Code Playgroud)
然后你的.NET代码应该可以正常工作.
该RETURN语句应仅用于状态代码,并且只能返回INT值.如果要使用它,则必须使用a SqlParameter为存储过程定义aDirection.ReturnValue
| 归档时间: |
|
| 查看次数: |
54792 次 |
| 最近记录: |