我通过Entity Framework Code-First中的存储过程实现实体。存储过程采用int参数并输出选择的记录和多个输出参数。无论出于何种原因,我从输出参数中获取Null,同时从SQL Management工作室执行Proc会导致预期的行为;所有输出参数都有值。我的实体正在实现,所以至少这是有效的......
SqlParameter DocsWithHits = new SqlParameter()
{
ParameterName = "DocumentsWithHits",
Direction = System.Data.ParameterDirection.Output,
Value = null,
SqlDbType = System.Data.SqlDbType.Int
};
SqlParameter TotalGB = new SqlParameter()
{
ParameterName = "TotalGb",
Direction = System.Data.ParameterDirection.Output,
Value = null,
SqlDbType = System.Data.SqlDbType.Float
};
ObservableCollection<SearchResult> ResultCollection;
ResultCollection = new ObservableCollection<SearchResult>(db.Database.SqlQuery<SearchResult>(
"exec ListSearchResultsWithTotals @SearchAnalysisID, @DocumentsWithHits, @RelatedDocuments, @TotalDocuments, @TotalGb, @DocumentsWithHitsNotExported, @RelatedDocumentsNotExported, @TotalDocumentsNotExported, @TotalGbNotExported",
new SqlParameter
{
ParameterName = "SearchAnalysisID",
Value = this.SearchAnalysisId
},
DocsWithHits,
TotalGB));
从SqlQuery方法生成的SQL如下所示。我在Profiler中捕获了它。执行时,我得到记录和空输出参数。
declare @p4 int
set @p4=NULL
declare @p5 float
exec sp_executesql N'exec ListSearchResultsWithTotals @SearchAnalysisID, @DocumentsWithHits, @TotalGb',N'@SearchAnalysisID int,@DocumentsWithHits int output,@TotalGb float output',@SearchAnalysisID=170,@DocumentsWithHits=@p4 output,@TotalGb=@p5 output
select @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11
我使用SqlParameter是错还是什么?
答案 0 :(得分:1)
您必须在传递给OUT
的SQL中使用SqlQuery
关键字(例如here)。在迭代或关闭查询的主结果集之后,还要确保您正在读取这些参数(应该由ObservableCollection
构造函数完成)。