0

I have a stored procedure like this and when I run this stored procedure in SQL Server Management Studio, I only get “Command(s) completed successfully.”</p>

How in my vb.net code with a simple if statement I can check if @onum is 0 do something.

I never work with this kind, usually stored procedures return an empty row when it doesn't have a value.

 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)

and I am calling this stored procedure in my vb.net code like this:

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
4

2 回答 2

0

Change your stored procedure to:

 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

this way you will have actual SELECT that will show results in Management Studio.

Then instead of last 4 lines after

command.Parameters.Add("@VendorID", SqlDbType.Int).Value = IIf(IsNothing(VENDORID), DBNull.Value, VENDORID)

that add RETURN_VALUE parameter and ExecuteNonQuery just do

RetVal = command.ExecuteScalar()
于 2013-08-17T00:14:40.053 回答
0

Problem seems to be in the declaration of the return parameter, as there is no need to give it a name or a type, since a return value is always an int. This question also has the same problem. The code they suggest would be something like this:

Dim RetvalParameter As SqlParameter = command.Parameters.Add()
RetvalParameter.Direction = ParameterDirection.ReturnValue

command.ExecuteNonQuery()
RetVal = CInt(RetvalParameter.Value)
于 2013-08-17T01:17:32.843 回答