【问题标题】:SQL Server Procedure Returning -1SQL Server 过程返回 -1
【发布时间】:2019-07-09 13:42:03
【问题描述】:

通过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 = 'Success' 返回 'Success' 或 'Error'

【问题讨论】:

  • 在这种情况下,与使用 ExecuteScalar 相比,使用返回参数可能有点矫枉过正。

标签: sql-server vb.net


【解决方案1】:

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

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

目前你得到 -1 ExecuteNonQuery() 返回受影响的行数 - 但你 SET NOCOUNT ON 它不会被填充。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-05
      • 1970-01-01
      • 2013-02-11
      • 2016-11-03
      • 2014-11-06
      • 1970-01-01
      相关资源
      最近更新 更多