简体   繁体   English

从EF4调用存储过程时返回值的问题

[英]Issue with return Value when calling stored procedure from EF4

I have written the following stored procedure in SQL Server 2008 : 我在SQL Server 2008中编写了以下存储过程:

ALTER Procedure [dbo].[usp_TodayNumberOfRegisteration] 
(
@TodayShamsiDate nvarchar
)
AS
Select COUNT(csci.Id) as cc1  FROM dbo.Complex_Service_Cart_Items csci INNER JOIN dbo.Complex_Service_Cart csc
ON csci.Id_Complex_Service_Cart=csc.Id
WHERE (csci.Id_Complex_Service='2cca1a67-34f4-4837-bebe-f3ba4c72b98d' or csci.Id_Complex_Service='8430cad2-dbb1-4425-bb8b-a7e158f688c4') 
and csc.TFIsPaymentComplete=1 
and csc.TFDateBackFromBankp= RTRIM( @TodayShamsiDate)

And I am calling it from C# codebehind via EF4 this way : 我通过这种方式通过EF4从C#codebehind调用它:

string shamsiDate = Date.getShamsiDate();
returnValue = Convert.ToString(db.getTodayNumberOfRegisteration(shamsiDate).First().Value);

where getTodayNumberOfRegisteration is a function I added to my edmx model . 其中getTodayNumberOfRegisteration是我添加到我的edmx模型的函数。

Now here is the issue : when I execute the stored procedure in SQL Server and instead of 现在问题是:当我在SQL Server中执行存储过程而不是

 and csc.TFDateBackFromBankp= RTRIM( @TodayShamsiDate)

I set something like : 我设置了类似的东西:

 and csc.TFDateBackFromBankp= RTRIM( '1391/12/05')

This stored procedure returns a value of 6 此存储过程返回值6

But when I pass the parameter from C# codebehind and I get the return value '0' 但是当我从C#codebehind传递参数时,我得到返回值'0'

Any help would be appreciated. 任何帮助,将不胜感激。

I normally do it like this: in the Add Function Import dialog, select your stored procedure and define that it returns a Collection Of Scalars: Int32 : 我通常这样做:在Add Function Import对话框中,选择你的存储过程并定义它返回一个Scalars集合:Int32

在此输入图像描述

Then in your code, call it like this: 然后在你的代码中,像这样调用它:

int value = db.getTodayNumberOfRegisteration(shamsiDate).First().Value;

This usually works just fine for me. 这通常对我来说很好。

If you don't define it as returns a collection of: Int32 , it seems that the value you're getting back is really the return value from the stored procedure call, eg the number of rows that were affected by the stored procedure execution (0 or -1 for a SELECT , since you didn't actually insert, update or delete any rows): 如果你没有将它定义为返回一个集合:Int32 ,那么你得到的值似乎是存储过程调用的返回值 ,例如受存储过程执行影响的行数(对于SELECT ,0或-1,因为您实际上没有插入,更新或删除任何行):

I found the issue : I had set the parameter this way : 我发现了这个问题:我以这种方式设置了参数:

@TodayShamsiDate nvarchar

and I should have specified the length of nvarchar 我应该指定nvarchar的长度

@TodayShamsiDate nvarchar(10)

I did It and the problem is solved ! 我做到了,问题解决了!

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

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