从VB.NET应用程序调用SQL Server存储过程。输出参数返回为空

从VB.NET应用程序调用SQL Server存储过程。输出参数返回为空,sql,.net,vb.net,stored-procedures,Sql,.net,Vb.net,Stored Procedures,我试图调用一个简单的存储过程,该过程从VB.NET应用程序返回一个输出参数,但当我试图显示结果时,它显示为空。代码没有抛出任何异常,当我在SQLServerMgmtStudio中执行SP时,它会像我预期的那样返回。我可能只是忽略了一些东西。感谢您的建议。SP和VB代码如下: SP代码: create procedure [dbo].[get_number_potential_matches] @deductnRecId int, @numberPotentialMatches int outpu

我试图调用一个简单的存储过程,该过程从VB.NET应用程序返回一个输出参数,但当我试图显示结果时,它显示为空。代码没有抛出任何异常,当我在SQLServerMgmtStudio中执行SP时,它会像我预期的那样返回。我可能只是忽略了一些东西。感谢您的建议。SP和VB代码如下:

SP代码:

create procedure [dbo].[get_number_potential_matches] @deductnRecId int, @numberPotentialMatches int output as 
begin 
    select numberPotentialMatches = count(*) 
    from potential_match pm 
    where pm.deductn_rec_id = @deductnRecId;
    return
end
VB.Net程序:

Sub getPotentialMatchCount(ByVal deductnRecId As Integer)
    Try
        SqlCmd = New SqlCommand("get_number_potential_matches", SqlConn)
        SqlCmd.CommandType = CommandType.StoredProcedure
        SqlCmd.Parameters.Add("@deductnRecId", SqlDbType.Int).Value = deductnRecId
        SqlCmd.Parameters("@deductnRecId").Direction = ParameterDirection.Input

        SqlCmd.Parameters.Add("@numberPotentialMatches", SqlDbType.Int)
        SqlCmd.Parameters("@numberPotentialMatches").Direction = ParameterDirection.Output

        SqlConn.Open()
        SqlCmd.ExecuteNonQuery()

    Catch ex As Exception
        lbl_pm.Text = ex.Message
    Finally
        SqlCmd.Dispose()
        SqlConn.Close()
    End Try
    lbl_pm.Text = "deductnRecId: " & deductnRecId.ToString & " has " & SqlCmd.Parameters("@numberPotentialMatches").Value.ToString() & " matches"
End Sub

SP中的参数名称中缺少“@”。它应该是

select @numberPotentialMatches = count(*) ...
您所做的是选择
count(*)
并将
numberPotentialMatches
设置为列别名


另外,在处理后不要访问
SqlCmd

您在Finally中处理SqlCmd对象,但随后尝试获取参数值。另一方面,你最好使用语句初始化你的连接和命令对象,这会隐式地处理对象。他在处理后也在使用SqlCmd对象,这是不好的。