PetaPoco输出参数来自存储过程不返回

时间:2013-01-13 08:23:27

标签: .net sql-server vb.net petapoco

使用带有最新PetaPoco的SQL Server 2005,代码如下:

    Dim count = New SqlParameter("@Count", System.Data.ParameterDirection.Output)
    count.DbType = Data.DbType.Int32

    Dim results = db.Query(Of User)(";exec SearchUserPaged @@page=@page, @@maximumRows=@maximumRows, @@ClientID=@ClientID, @@orderBy=@SortExp, @@Count=@Count OUT",
                                    New With {.page = pageEx, .maximumRows = maximumRowIndex, .ClientID = Me.ClientID, .SortExp = sortExpression, .Count = count}).ToList()

    'Dim results = db.EDPEntities.Query(Of User)(";exec SearchUserPaged @@page=@0, @@maximumRows=@1, @@ClientID=@2, @@orderBy=@3, @@Count=@4 OUTPUT",
    '                                  pageEx, maximumRowIndex, Me.ClientID, sortExpression, count).ToList()

    If IsDBNull(count.Value) Then
        Me.Count = 0
    Else
        Me.Count = count.Value
    End If

但是输出参数总是返回值0.对于PetaPoco来说很新,所以不确定我是否遗漏了一些非常明显的东西。

这是Peta生成的SQL,它在查询分析器中运行良好:

DECLARE @0 int,@1 int,@2 int,@3 nvarchar(4000),@4 int
SET @0=1
SET @1=25
SET @2=10145
SET @3=NULL
SET @4=2
exec SearchUserPaged @page=@0, @maximumRows=@1, @ClientID=@2, @orderBy=@3, @Count=@4 OUT

SELECT @4

当我SELECT @4时,返回正确的值。

2 个答案:

答案 0 :(得分:0)

这有效(不确定原因):

    Dim count = New SqlParameter("@Count", System.Data.SqlDbType.Int)
    count.Direction = System.Data.ParameterDirection.Output
    count.Value = DBNull.Value

    Dim results = db.Query(Of User)(";exec SearchUserPaged @@page=@page, @@maximumRows=@maximumRows, @@ClientID=@ClientID, @@orderBy=@SortExp, @@Count=@Count OUT", New With { _
        Key .page = pageEx, _
        Key .maximumRows = maximumRowIndex, _
        Key .ClientID = ClientID, _
        Key .SortExp = sortExpression, _
        Key .Count = count _
    }).ToList()

    If IsDBNull(count.Value) Then
        Me.Count = 0
    Else
        Me.Count = count.Value
    End If

答案 1 :(得分:0)

我认为这是因为Param的默认值

Dim count = New SqlParameter("@Count", System.Data.SqlDbType.Int)
count.Direction = System.Data.ParameterDirection.Output
count.Value = DBNull.Value -- this fixed the error.

希望这有帮助