使用两个出站参数从VB 2008调用sql server存储过程

时间:2015-06-03 22:35:58

标签: sql-server vb.net

存储过程是返回记录集和两个结果。 从SQL Server管理工作室执行时,它返回记录集和@NumItems的值以及@Reason的值。但是,当从VB调用时,@Reason会为@NumItems获得0,但应该返回@@rowcount,即{4}。 @Reason的回复值为“OK”,但@NumItems将返回为0。

SProc结束

set @NumItems = @@ROWCOUNT
set @Reason = 'OK' -- process completed successfully
at end. The recordset when executed has four rows and @NumItems has returnvalue of 4 in SQL SERVER

有什么想法吗?

Dim dt As New DataTable
    Dim da As New SqlClient.SqlDataAdapter
    Dim strConnection As String = ""
    strConnection = strConnectionInfo
    Dim sqlCommand As New SqlCommand
    Dim sqlConnection As New SqlConnection
    sqlConnection.ConnectionString = strConnection
    sqlConnection.Open()
    Try
        With sqlCommand
            .CommandType = CommandType.StoredProcedure
            .CommandText = "dbo.spRestockMinMax"
            .Parameters.Add("@NumItems", SqlDbType.BigInt).Direction = ParameterDirection.Output
            .Parameters.Add("@Reason", SqlDbType.VarChar, 20).Direction = ParameterDirection.Output
            .Connection = sqlConnection
        End With
        sqlCommand.ExecuteScalar()
        'Dim NumItems As Long = sqlCommand.Parameters("@NumItems").Value.ToString
        da.SelectCommand = sqlCommand
        da.Fill(dt)
        dv = New DataView(dt)
        Me.dgMinMaxReplenish.DataSource = dv
        Dim myReason As String = sqlCommand.Parameters("@Reason").Value.ToString
        Dim NumItems As Long = sqlCommand.Parameters("@NumItems").Value.ToString
        If UCase(myReason) = "OK" Then
            If NumItems > 0 Then
                MsgBox("Replenishment List Created with " & NumItems & " added for replenishment", MsgBoxStyle.Information, "Replenishment List Created")
            Else
                MsgBox("No Items found needing replenishment", MsgBoxStyle.Information, "No Items Needed")
            End If
        Else
            MsgBox("Something happenned during stored procedure", MsgBoxStyle.Critical)
        End If
    Catch ex As Exception
        txtSystemMessages.Text = ex.Message
    End Try
    If dgMinMaxReplenish.RowCount > 1 Then
        dgMinMaxReplenish.Visible = True
    End If

1 个答案:

答案 0 :(得分:1)

我不知道你的存储过程在内部做了什么,但是你运行了两次。第一次在ExecuteScalar调用中,第二次在Fill调用中,并在第二次调用后读取输出参数。当按此顺序调用时,该过程可能会生成不同的输出,而不会对基础数据进行任何更改。

所以,也许你可以简单地删除对ExecuteScalar的调用并直接运行Fill调用