简体   繁体   English

无法读取使用实体框架从存储过程返回的值

[英]Unable to read value returned from a stored procedure using Entity Framework

I have written a stored procedure that will return me ID of a row if found and if not found, will create a row and return ID of the newly created row. 我编写了一个存储过程,如果找到该行,将返回行的ID;如果未找到,将创建行,并返回新创建的行的ID。 I am facing problems in retrieving this returned value in my C# function. 我在C#函数中检索此返回值时遇到问题。


The Stored Procedure: 存储过程:

PROCEDURE [TagDatabase].[GetTagID] 
    -- Add the parameters for the stored procedure here
    @TagName as varchar(200), 
    @UserID int = 0,
    @TagID int = 0 OUTPUT
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    select @TagID=ID from Tag where Name = @TagName and UserID=@UserID;
    IF NULLIF(@TagID, '') IS NULL
    BEGIN
        DECLARE @OutputTbl TABLE (ID INT)

        INSERT INTO Tag(Name, ParentID, UserID)
        OUTPUT INSERTED.ID INTO @OutputTbl(ID)
        VALUES (@TagName, 0, @UserID);

        select @TagID=ID from @OutputTbl


    END

    select @TagID
END

My function to retrieve the value returned by this: 我的函数来检索由此返回的值:

ObjectParameter objParam = new ObjectParameter("TagID", typeof(long));

context.GetTagID("newTag", 10, objParam);
context.SaveChanges();
long id = Convert.ToInt64(Convert.ToInt64(id)));

Change 更改

long id = Convert.ToInt64(Convert.ToInt64(id))); 长id = Convert.ToInt64(Convert.ToInt64(id)));

to

long id = Convert.ToInt64(objParam.Value); 长id = Convert.ToInt64(objParam.Value);

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

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