[英]How can I use the OUT parameter from a SQL Server stored procedure in a vb.net code
[英]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.