未返回ExecuteStoreQuery输出参数

时间:2012-08-02 16:22:04

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

我有一些实体框架代码来运行一个返回参数的存储过程。每次运行代码时,参数都会返回null。有没有人有任何想法可能导致这个?

由于

代码:

SqlParameter Business = new SqlParameter("Business", Search.Term);
                SqlParameter Location = new SqlParameter("Location", Search.Location);
                SqlParameter PageNumber = new SqlParameter("PageNumber", Search.CurrentPage);
                SqlParameter RecordsPerPage = new SqlParameter("RecordsPerPage", Search.RecordsPerPage);                  


                var TotalRecords = new SqlParameter
                {
                    ParameterName = "TotalRecords",
                    Value = 0,
                    Direction = ParameterDirection.Output                        
                };



                var List = db.ExecuteStoreQuery<ENT_SearchBusinessResult>("exec usp_BusinessUser_Search @Business,@Location,@PageNumber,@RecordsPerPage,@TotalRecords out", Business, Location, PageNumber, RecordsPerPage, TotalRecords); 

我使用了Sql profiler,发现它正在执行以下操作:

declare @p7 int
set @p7=53
exec sp_executesql N'exec usp_BusinessUser_Search    @Business,
@Location,@PageNumber,@RecordsPerPage,
@TotalRecords out',
N'@Business  nvarchar(14),@Location nvarchar(14),
@PageNumber int,
@RecordsPerPage int,@TotalRecords int output',
@Business=N'Food 
and Drink',@Location=N'United   Kingdom',@PageNumber=1,@RecordsPerPage=10,
@TotalRecords=@p7 output
select @p7

这很好,如果你运行这个查询但它没有返回我的代码,它会显示返回值:(

2 个答案:

答案 0 :(得分:1)

This article表示您需要在尝试访问out参数的值之前阅读所有结果。您的代码不够完整,无法判断您是否正在执行此操作(如果不是,请尝试ToList()。)

答案 1 :(得分:0)

这不一定是真的。这是我的输出参数的工作示例:

                            private IEnumerable<AppealsGridViewModel> GetDataNew(string sortString, int pageNumber, int pageSize)
                    {
                        var totalRowCount = new SqlParameter
                        {
                            ParameterName = "@TotalRowCount",
                            SqlDbType = SqlDbType.Int,
                            Direction = ParameterDirection.Output
                        };

                        var totalPropertyCount = new SqlParameter
                        {
                            ParameterName = "@TotalPropertyCount",
                            SqlDbType = SqlDbType.Int,
                            Direction = ParameterDirection.Output
                        };

                        var paramList = new object[] {
                            new SqlParameter {ParameterName = "@CompanyId", SqlDbType = SqlDbType.Int, Value = 5}
                            , new SqlParameter {ParameterName = "@AccountId", SqlDbType = SqlDbType.Int, Value = 0}
                            , new SqlParameter {ParameterName = "@IsServiceCenterUser", SqlDbType = SqlDbType.Bit, Value = 0}
                            , new SqlParameter {ParameterName = "@PageNumber", SqlDbType = SqlDbType.Int, Value = (pageNumber + 1)}
                            , new SqlParameter {ParameterName = "@PageSize", SqlDbType = SqlDbType.Int, Value = pageSize}
                            , new SqlParameter {ParameterName = "@SortOrder", SqlDbType = SqlDbType.VarChar, Value = sortString}
                            , totalRowCount
                            , totalPropertyCount
                        };

                        var s = _dataContext.ExecuteStoreQuery<AppealsGridViewModel>("exec atAppealsSearch @CompanyId, @AccountId, @IsServiceCenterUser, @PageNumber, @PageSize, @SortOrder, @TotalRowCount out, @TotalPropertyCount out", paramList).ToList();

                        var appealCount = totalRowCount.Value;
                        var propertyCount = totalPropertyCount.Value;

                        return s;
                    }

            ALTER PROCEDURE [dbo].[atAppealsSearch]
                @CompanyId INT
                , @AccountId INT = 0
                , @IsServiceCenterUser BIT = 0
                , @PageNumber INT = 1
                , @PageSize INT = 25
                , @SortOrder VARCHAR(MAX)
                , @TotalRowCount int OUT
                , @TotalPropertyCount int OUT
                AS


            SELECT @TotalRowCount = 33124
            SELECT @TotalPropertyCount = 555

            EXEC SP_EXECUTESQL @Query