簡體   English   中英

從VB.NET應用程序調用SQL Server存儲過程。 輸出參數恢復為空白

[英]Calling SQL Server stored procedure from VB.NET application. Output parameter is coming back blank

我試圖調用一個簡單的存儲過程,該存儲過程從VB.NET應用程序返回OUTPUT參數,但是當我嘗試顯示結果時,它顯示為空白。 該代碼不會引發任何異常,當我在SQL Server Mgmt studio中執行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完后不要訪問SqlCmd

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM