我编写了一个存储过程,如果找到该行,将返回行ID,如果找不到,将创建一行并返回新创建的行的ID。我在C#函数中检索此返回值时遇到问题。
存储过程:
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
我的函数检索由此返回的值:
ObjectParameter objParam = new ObjectParameter("TagID", typeof(long));
context.GetTagID("newTag", 10, objParam);
context.SaveChanges();
long id = Convert.ToInt64(Convert.ToInt64(id)));
答案 0 :(得分:0)
更改
long id = Convert.ToInt64(Convert.ToInt64(id)));
到
long id = Convert.ToInt64(objParam.Value);