SQL Server过程返回-1

时间:2019-06-26 16:38:28

标签: sql-server vb.net

通过VB.Net执行SQL Server存储过程存储过程正确执行并返回值。但是,通过VB.Net执行该程序时,其返回值是-1。

VB.NET的源代码

Dim cmdSPStagingToPROD As New SqlCommand
Dim cnStagingToPROD As SqlConnection
Dim SPResponse As String

cmdSPStagingToPROD.CommandType = CommandType.StoredProcedure

cmdSPStagingToPROD.Parameters.Add("@responseMessage", SqlDbType.NVarChar, 250)
cmdSPStagingToPROD.Parameters("@responseMessage").Direction = ParameterDirection.Output

cnStagingToPROD = New SqlConnection(ConnectionString)
cnStagingToPROD.Open()

cmdSPStagingToPROD.Connection = cnStagingToPROD

SPResponse = cmdSPStagingToPROD.ExecuteNonQuery()

If SPResponse = "Success" Then
    StagingToPROD = "Success"
Else
    StagingToPROD = "Error"
    ErrorText = "Staging to PROD error " & SPResponse
End If

期望SET @responseMessage ='成功'返回'成功'或'错误'

2 个答案:

答案 0 :(得分:3)

您将值分配给输出参数,以便在执行后获取该值,您需要阅读:

cmdSPStagingToPROD.Parameters("@responseMessage").Value.ToString()

当前,您得到-1,因为ExecuteNonQuery()返回了受影响的行数-但是当您SET NOCOUNT ON时,它不会被填充。

答案 1 :(得分:0)

执行命令后,您需要读取输出参数@responseMessage
这样:

cmdSPStagingToPROD.Execute()
SPResponse = cmdSPStagingToPROD.Parameters("@responseMessage").Value