【问题标题】:Stored Procedure Returning False in vb.net存储过程在 vb.net 中返回 False
【发布时间】:2014-01-16 21:00:01
【问题描述】:

我的 vb.net 程序有问题。问题来自我的存储过程类。我已经测试了我正在使用的 SQL 数据库中的存储过程,并且它们都在工作。

我还想指出,我确实将 SP 的名称复制并粘贴到 SQL 服务器并执行它,它运行良好。

EXEC dbo.ksp_Get_Available_Statuses

在程序本身中只有一个 SP 类在工作。以下是该代码:

Imports System.Data.SqlClient

Public Class Add_Term
    Public Function addTerm(ByVal term As String, ByVal definitionSource As Integer, ByVal formatNote As String, ByVal definition As String, ByVal authorization As String, ByVal addReason As String)
        Dim connection As SqlConnection = DataConnection.getProperityDBConnection

        Dim insertCommand As New SqlCommand("dbo.ksp_Add_Term", connection)
        insertCommand.CommandType = CommandType.StoredProcedure
        insertCommand.Parameters.AddWithValue("@term", term)
        insertCommand.Parameters.AddWithValue("@definitionSource", definitionSource)
        insertCommand.Parameters.AddWithValue("@formatNote", formatNote)
        insertCommand.Parameters.AddWithValue("@definition", definition)
        insertCommand.Parameters.AddWithValue("@authorization", authorization)
        insertCommand.Parameters.AddWithValue("@addReason", addReason)

    Try
        connection.Open()
        Dim count As Integer = insertCommand.ExecuteNonQuery()
        If count > 0 Then
            Return True
        Else
            Return False
        End If
    Catch ex As Exception
        Throw ex
    Finally
        connection.Close()
    End Try
 End Function
End Class

这是我正在修复的代码,但它不起作用。我看不出我做错了什么,这让我发疯。我需要第二双(如果不是更多的话)眼睛。 导入 System.Data.SqlClient

Public Class Get_Avaliable_Statuses
    Public Function getAvailableStatuses()
        Dim connection As SqlConnection = DataConnection.getProperityDBConnection

        Dim insertCommand As New SqlCommand("dbo.ksp_Get_Available_Statuses", connection)
        insertCommand.CommandType = CommandType.StoredProcedure

        Try
            connection.Open()
            Dim count As Integer = insertCommand.ExecuteNonQuery()
            If count > 0 Then
                Return True
            Else
                Return False
            End If
        Catch e As Exception
            Throw e
        Finally
            connection.Close()
        End Try
    End Function
End Class

以下是为那些想知道确切措辞的人弹出的错误: Error Message Screen Shot. 如允许,任何帮助表示赞赏。

【问题讨论】:

  • 你确定这两个 procs 在同一个数据库中吗?检查this
  • 是的。两个 SP 都使用相同的数据连接类。它们都在 SQL 服务器中工作,但只有其中一个在代码中工作。我不知道为什么它一直将第二个 SP 返回为假。它有包含结果的行。

标签: sql sql-server vb.net stored-procedures sqlcommand


【解决方案1】:

你有没有

set nocount on

在你的存储过程中?我猜 ExecuteNonQuery 不能返回受影响的行数然后......

【讨论】:

  • 老板查看了我的代码。发现它应该读取 ExecuteReader 而不是 ExecuteNonQuery。不过感谢您的帮助!
【解决方案2】:

主管查看了我的代码以提供帮助。意识到它应该是 Excute Reader 而不是 ExecuteNonQuery。我在寻找信息,而不是插入新的来源。

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    相关资源
    最近更新 更多