简体   繁体   English

如何使用linq和C#获取存储过程的返回值?

[英]How to get stored procedure return value using linq with C#?

have a stored Procedure that do some operation and return 1 or 0 as below. 具有执行某些操作并返回如下值的1或0的存储过程。

CREATE PROCEDURE dbo.UserCheckUsername11
(
    @Username nvarchar(50)
)
AS
BEGIN
SET NOCOUNT ON;

IF Exists(SELECT UserID FROM User WHERE username =@Username) 
    return 1
ELSE
    return 0
END

Using linq I tried to get return value. 我使用linq尝试获取返回值。 But i am getting the output as -1 always. 但是我总是将输出设为-1。

Below is my Linq code : 以下是我的Linq代码:

 using (PlanGenEntities3 entity2 = new PlanGenEntities3())
 {                
    var result = entity2.Entity_test_GetHoliday();
    string output = result.ToString();
 }

How to solve this ? 如何解决呢?

Just declare your scalar variable as a output parameter for SQL query, then in the code you could retrieve its value this way: ``` 只需将标量变量声明为SQL查询的输出参数,然后在代码中就可以通过以下方式检索其值:

using (PlanGenEntities3 myContext = new PlanGenEntities3())            
{                
   var outputParameter = new System.Data.Objects.ObjectParameter("OutputParameterName", typeof(int));                
   myContext.Entity_test_GetHoliday(outputParameter );                
   Console.WriteLine(outputParameter.Value);            
}

``` ```

I suppose there is way to access the default scalar output of your SP in the similar manor. 我想有一种方法可以在类似的庄园中访问SP的默认标量输出。

If he had it as an output parameter it would automatically show as a reference parameter of the proper corresponding data type in the LINQ call. 如果他将其作为输出参数,它将在LINQ调用中自动显示为适当的相应数据类型的参考参数。

He wouldn't really need to create a parameter object to contain that. 他实际上不需要创建参数对象来包含该对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM