【发布时间】:2017-01-10 03:18:51
【问题描述】:
我在我的 ASP.NET 项目中为实体框架使用数据库优先方法。我有一个存储过程,它有一个输出参数@TotalCount,它使用SELECT 语句返回一个结果集。
当我在我的MyDbContext.edmx 文件中添加存储过程时,MyDbContext.cs 有一个函数返回一个为存储过程自动生成的 int(可能是输出参数)。
如何使用这种方法访问输出参数和结果集?
下面给出了存储过程sn-p。基本上我正在尝试在存储过程中进行分页。
CREATE PROCEDURE [dbo].[sp_GetDetailsForStudent]
@StudentId BIGINT,
//....
@OrderBy NVARCHAR(max),
@Page INT OUTPUT,
@Items INT = 200,
@TotalCount INT OUTPUT
//.....
SET @SortSql = N'SELECT * FROM #tmpTable'
//......
EXEC sp_executesql @SortSql;
在MyDbContext.cs 文件中
public virtual int sp_GetDetailsForStudent(parameters ...){
//......
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("sp_GetDetailsForStudent", input parameters..., totalCount);
}
【问题讨论】:
-
希望link 对您有所帮助。
-
旁注:您应该不为您的存储过程使用
sp_前缀。微软有reserved that prefix for its own use (see Naming Stored Procedures),你确实会在未来某个时候冒着名称冲突的风险。 It's also bad for your stored procedure performance。最好只是简单地避免sp_并使用其他东西作为前缀 - 或者根本不使用前缀!
标签: c# .net sql-server asp.net-mvc stored-procedures