在插入时从SQL Server获取SCOPE_IDENTITY

我想已经太晚了,我太累了,看不出我做错了什么。这是我正在尝试的:
int imageId = imageDal.AddImage(new SqlParameter[]
        {
            new SqlParameter("@IMAGE_ID", 
        SqlDbType.Int, Int32.MaxValue, ParameterDirection.Output,
        true, 0, 0,"IMAGE_ID", DataRowVersion.Current,DBNull.Value),

        new SqlParameter("@IMAGE", 
        SqlDbType.Image, 11, ParameterDirection.Input,
        true, 0, 0,"IMAGE", DataRowVersion.Current,image)
        });

public int AddImage(SqlParameter[] spParams)
{
    SqlHelper.ExecuteNonQuery(BaseDAL.ConnectionStringImages, INSERT_IMAGE_SQL, spParams);
    return Convert.ToInt32(spParams[0].Value);
}
存储过程:
[dbo].[sp_insert_image]
    -- Add the parameters for the stored procedure here
    @IMAGE_ID int OUT,
    @IMAGE image
AS
BEGIN
    INSERT INTO images
    (IMAGE)
    VALUES
    (@IMAGE)
    SELECT @IMAGE_ID = SCOPE_IDENTITY();
END
GO
我得到DBNull为spParams [0] .Value。我已经尝试将@IMAGE_ID的值设置为我的存储过程中的常量但它没有改变任何东西所以问题不在于我的存储过程(这就是我的想法)。 当我从sql management studio执行该过程时,我看到inserted_id返回..     
已邀请:
我最终得到了执行标记并直接从SP返回
SCOPE_IDENTITY()
。 如果还有其他方法可以实现并从sql参数中获取值,我很乐意听到这个消息。     
我想,问题出在ExecuteNonQuery方法中。它返回对象数组,而不是sqlparam的数组。 只需在输出sqlparam对象上有一个ref,并从该ref获取值。 祝好运! 附:还有另一种解决方案。检查链接 http://www.dotnetmonster.com/Uwe/Forum.aspx/dotnet-distributed-apps/160/Data-Access-Application-Block-Output-Parameters     
你喜欢这个....
[dbo].[sp_insert_image]
    -- Add the parameters for the stored procedure here
    @IMAGE_ID int OUT,
    @IMAGE image
AS
BEGIN
    INSERT INTO images (IMAGE) VALUES (@IMAGE)
    SET @IMAGE_ID  = SCOPE_IDENTITY();
END
GO
    

要回复问题请先登录注册