从存储过程中获取输出参数的值

时间:2017-09-19 16:59:03

标签: vb.net entity-framework stored-procedures

我试图从我的数据库管理员写的存储过程中获取输出参数。这是存储过程。

ALTER PROCEDURE [dbo].[sb_AddStudent]
    -- Add the parameters for the stored procedure here

    @FirstName varchar(25),
    @LastName varchar(25),
    @Address varchar(300),
    @SID int output
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    BEGIN TRANSACTION

INSERT INTO StudentMaster(EntryDate, FirstName, LastName)
        VALUES (getdate(), @FirstName , @LastName);
        IF @@ERROR <> 0
            GOTO ErrorHandler;

        SELECT @SID= max(SID) 
        FROM StudentMaster

COMMIT TRANSACTION
    RETURN 0;


    -- if there is an error after any insert/update statement, it will go here --
    ErrorHandler:
        ROLLBACK TRANSACTION;
        RETURN @@error;             
END

这是我在vb.net中的代码。我正在使用实体框架。

 Dim outputParameter As Entity.Core.Objects.ObjectParameter = New Entity.Core.Objects.ObjectParameter("SID ", GetType(Integer))

db.sb_AddStudent(FirstName, LastNmae, Address, outputParameter)

dim SID = outputParameter.Value

我没有收到任何错误,数据保存到数据库但我不知道什么是从存储过程中获取输出值的正确方法。请建议获取输出参数值的最佳方法。

2 个答案:

答案 0 :(得分:0)

删除存储过程表单EF并将其重新添加到项目中为我解决了问题。

答案 1 :(得分:0)

删除存储过程,创建一个复合并在逻辑中绑定这样的输出值:

    public List<CompOffModel> GetCompOffRecords(string searchKeyword, string orderBycolumn, string orderByDirection, int start, int length, string StartYearFrom, string EndYearTo, string ApprovedBy, string CreatedBy, string DateFrom, string DateTo, string Name, string Reason, string IsUsed, ref int totalcount, ref int searchCount)
    {
        ObjectParameter objCountParam = new ObjectParameter("outputParameterOfSqlProcedure", 0);
        ObjectParameter objSearchCountParam = new ObjectParameter("outputParameterOfSqlProcedure1", 0);
        List<CompOffModel> result = db.SP_CompOff(searchKeyword, orderBycolumn, orderByDirection, start, length, StartYearFrom, EndYearTo, ApprovedBy, CreatedBy, DateFrom, DateTo, Name, Reason, IsUsed, objCountParam, objSearchCountParam).Select(x => new CompOffModel()
        {
            ID = x.ID,
            Name = x.Name,
            ApprovedBy = x.Approved_By,
            CreatedByString = x.Created_By,
            Reason = x.Reason,
            Date = x.Date,
            IsUsed = x.IsUsed,
            NoofCompOff = x.NoOfCompOff
        }).ToList();

        totalcount = Convert.ToInt32(objCountParam.Value);
        searchCount = Convert.ToInt32(objSearchCountParam.Value);
        return result;
    }