为什么存储过程中的输出参数是从Entity Null调用的?

时间:2015-02-22 22:36:14

标签: c# sql-server entity-framework stored-procedures output-parameter

我正在调用存储过程并声明输出参数:

CREATE PROCEDURE dbo.usp_getIsReadyForProcess
@VideoId INT ,
@val INT OUTPUT
AS
BEGIN
BEGIN TRY
    BEGIN TRANSACTION
-- LOCK ROW UNTIL END OF TRANSACTION
        SELECT * FROM dbo.ProcessStatus WITH (ROWLOCK, HOLDLOCK) WHERE VideoId = @VideoId

然后在整个交易中设置值

  --If there is no row count
IF @@ROWCOUNT = 0
BEGIN
    SET @val = 0
END

-- If video is already in process
ELSE IF @statusCode > 1
BEGIN
    SET @val = 0
END

...... more if blocks

          -- RELEASE LOCK
    COMMIT TRANSACTION
END TRY   

以下是获取输出参数的c#代码:

        using (var db = EntityFactory.GetInstance())
        {
            ObjectParameter objParam = new ObjectParameter("val", typeof(int));
            db.usp_getIsReadyForProcess(videoId, objParam);

            return (int)objParam.Value == 1;
        }

......但是无论我做什么,objParam.Value都是null。

所以我深入挖掘并发现已经处理的异常,

消息: 只能在Type.IsGenericParameter为true的Type上调用方法。

我做错了什么?我尝试了类型(bool,int32,string)..没有任何作用

1 个答案:

答案 0 :(得分:1)

由于以下行,您的存储过程返回结果集:

SELECT * FROM dbo.ProcessStatus WITH (ROWLOCK, HOLDLOCK) WHERE VideoId = @VideoId

您可以将SQL更改为:

DECLARE @RowCount INT
SELECT @RowCount = COUNT(*) FROM dbo.ProcessStatus WITH (ROWLOCK, HOLDLOCK) WHERE VideoId = @VideoId

或者您可以在代码中捕获结果集:

var resultSet = db.usp_getIsReadyForProcess(videoId, objParam);