C# 如何使用DataContext.ExecuteCommand并获取已执行的存储过程返回值?

C# 如何使用DataContext.ExecuteCommand并获取已执行的存储过程返回值?,c#,asp.net-mvc-3,sql-server-2008,stored-procedures,return-value,C#,Asp.net Mvc 3,Sql Server 2008,Stored Procedures,Return Value,在c项目中,我按如下方式调用存储过程: System.Data.Linq.DataContext dataContext = MembershipContext.GetContext(connectionString); int returnValue = dataContext.ExecuteCommand("EXEC usp_SomeProcedure {0}, {1}, {2}", param1, param2, param3); 但是,ExecuteCommand返回受影响的行数,而不

在c项目中,我按如下方式调用存储过程:

System.Data.Linq.DataContext dataContext = MembershipContext.GetContext(connectionString);
int returnValue = dataContext.ExecuteCommand("EXEC usp_SomeProcedure {0}, {1}, {2}", param1, param2, param3);
但是,ExecuteCommand返回受影响的行数,而不是我的存储过程返回值。获取此值的最简单方法是什么。我之所以需要它,是因为SP在成功时返回0,如果发生错误则返回正的int值


目前,存储过程使用RETURN输出其返回值。但是,我可以将其更改为SELECT,也可以根据需要使用输出参数。

我认为您需要将CommandType设置为CommandType.StoredProcedure,以便从存储过程中获取返回值。请参见此处接受的答案:

如果不需要Linq2Sql映射功能,请不要使用DataContext调用SPs。只需使用SqlCommand as.

我也使用了您提到的页面上给出的示例,它正在工作。我不再像Serg Rogovtsev建议的那样使用DataContext+祝你们两个都快乐!