0

我通过 Entity Framework Code-First 中的存储过程实现实体。存储过程接受一个 int 参数并输出记录的选择和几个输出参数。无论出于何种原因,在从 SQL Management Studio 执行 Proc 时,我的输出参数返回 Null 会导致预期的行为;所有输出参数都有值。我的实体正在被物化,所以至少那是有效的……

英孚代码

        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));

SQL 探查器

下面是从 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 错误还是什么?

4

1 回答 1

1

您必须OUT在传递给的 SQL 中使用关键字SqlQuery此处为示例)。还要确保在迭代或关闭查询的主要结果集后读取这些参数(应该由ObservableCollection构造函数完成)。

于 2012-01-13T15:19:18.790 回答