我有一个这样的存储过程,当我在SQL Server Management Studio中运行此存储过程时,我只获得“命令已成功完成。”
如果在我的vb.net代码中使用简单的if语句,我可以检查@onum
是否为0。
我从不使用这种类型,通常存储过程在没有值时返回空行。
ALTER PROCEDURE [dbo].[o_By_RNum]
@rNum varchar(50)
,@ServiceType varchar(50)
,@Direction varchar(1) = NULL
,@VendorID int
AS
Declare @onum int
If @Direction is Null
Begin
SELECT @onum = o_num
FROM RemoteOrder
WHERE r_num LIKE @rNum
AND r_ServiceType = @ServiceType
AND VendorID = @VendorID
End
Else
Begin
SELECT @onum = o_num
FROM RemoteOrder
WHERE r_num LIKE @rNum
AND r_ServiceType = @ServiceType
AND Direction = @Direction
AND VendorID = @VendorID
End
RETURN ISNULL(@onum,0)
我在我的vb.net代码中调用这个存储过程,如下所示:
Private Sub DLL_GetOnumFromRemoteOrder_By_RNum(ByRef SqlConn As SqlConnection, ByRef R_NUM As String, ByRef R_SERVICETYPE As String, ByRef DIRECTION As String, ByRef VENDORID As Integer, ByRef RetVal As Integer)
'Open the SQL Connection
If SqlConn.State <> ConnectionState.Open Then SqlConn.Open()
'Add Parameters
Dim command As New SqlCommand("DLL_GetOnumFromRemoteOrder_By_RNum", SqlConn)
command.CommandType = CommandType.StoredProcedure
command.Parameters.Add("@rNum", SqlDbType.VarChar).Value = IIf(IsNothing(R_NUM), DBNull.Value, R_NUM)
command.Parameters.Add("@ServiceType", SqlDbType.VarChar).Value = IIf(IsNothing(R_SERVICETYPE), DBNull.Value, R_SERVICETYPE)
command.Parameters.Add("@Direction", SqlDbType.Char).Value = IIf(IsNothing(DIRECTION), DBNull.Value, DIRECTION)
command.Parameters.Add("@VendorID", SqlDbType.Int).Value = IIf(IsNothing(VENDORID), DBNull.Value, VENDORID)
command.Parameters.Add("@RETURN_VALUE", SqlDbType.Int)
command.Parameters("@RETURN_VALUE").Direction = ParameterDirection.ReturnValue
'Dim dr As SqlDataReader
'dr = command.ExecuteReader()
'dr.Read()
'Return dr
'Execute the command
RetVal = command.ExecuteNonQuery()
RetVal = CInt(command.Parameters.Item("@RETURN_VALUE").Value)
End Sub
答案 0 :(得分:0)
将存储过程更改为:
ALTER PROCEDURE [dbo].[o_By_RNum]
@rNum varchar(50)
,@ServiceType varchar(50)
,@Direction varchar(1) = NULL
,@VendorID int
AS
BEGIN
SELECT ISNULL(o_num,0)
FROM RemoteOrder
WHERE r_num LIKE @rNum
AND r_ServiceType = @ServiceType
AND (@Direction is Null OR Direction = @Direction)
AND VendorID = @VendorID
END
这样您将拥有将在Management Studio中显示结果的实际SELECT。
然后代替
之后的最后4行command.Parameters.Add("@VendorID", SqlDbType.Int).Value = IIf(IsNothing(VENDORID), DBNull.Value, VENDORID)
添加RETURN_VALUE参数和ExecuteNonQuery只需
RetVal = command.ExecuteScalar()
答案 1 :(得分:0)
问题似乎出现在return参数的声明中,因为没有必要给它一个名字或类型,因为返回值总是一个int。 This question也有同样的问题。他们建议的代码是这样的:
Dim RetvalParameter As SqlParameter = command.Parameters.Add()
RetvalParameter.Direction = ParameterDirection.ReturnValue
command.ExecuteNonQuery()
RetVal = CInt(RetvalParameter.Value)