【问题标题】:SQL query with parameter returning 0 instead of 475参数返回 0 而不是 475 的 SQL 查询
【发布时间】:2019-08-29 18:18:47
【问题描述】:

我不明白为什么下面的代码返回 0 而不是 475:

Public Function getSectionLength(sectionUID As Integer) As Integer
    Dim sectionLength As Integer = 0
    Using dr As New DataReader(globals.dif)
        Dim SQL As String = "SELECT dbo.SA.SECTION_LENGTH 
FROM dbo.SA WHERE dbo.SA.SECTION_UID = 
@sectionUid"
        Dim paramList As New List(Of SqlClient.SqlParameter)
        paramList.Add(New SqlClient.SqlParameter("@sectionUid", 
sectionUID))
        dr.ExecuteReader(SQL, paramList)
        If dr.Read Then
            sectionLength = dr("SECTION_LENGTH")
        End If
    End Using
    Return sectionLength
End Function

以下是变量的值:

sectionUID = 38

当我在 SSMS 中运行 SQL 查询并将 @sectionUid 交换为 38 时,我得到:

SECTION_LENGTH = 475

但是

dr.Read = False

dr.Read 怎么可能是假的?

编辑:这已经解决了。这个问题与 globals.dif 有关。它首先被初始化,但随后在程序点击此函数之前更改了值,导致错误。我通过在 getSectionLength 函数中重新初始化差异来解决它。

【问题讨论】:

  • 您是否真正调试过您的代码,即设置断点并逐行执行?看起来dr.ReadFalse,但您应该已经知道这一点,因为您应该在发布之前调试您的代码。
  • 是的,我有,但这并没有改变我不明白如果它在 using datareader 语句中它怎么可能是假的事实
  • 因为如果没有更多行它会返回false...你调试了吗?
  • 如果在代码中使用 38 代替 @sectionUid 会发生什么?另外,我还没有见过这种使用阅读器的方法
  • 如果我将@sectionUid 换成 38,也会发生同样的事情,dr.ExecuteReader 仍然是 False。我已经调试过了,当我在 SSMS 中运行该 sql 语句时,它肯定会返回 SECTION_LENGTH = 475

标签: sql vb.net parameter-passing


【解决方案1】:

我不知道你从哪里得到这个函数的模式,但它很混乱。显然您正在尝试连接到 Sql Server 数据库,但我在您的代码中看不到任何连接。

首先,让我们检查一下您的代码。

'Good name for your function
        Public Function getSectionLength(sectionUID As Integer) As Integer
        Dim sectionLength As Integer = 0
        'The DataReader constructor does take any arguments.
        'You should be using an SqlDataReader
        'Normally you do not need a New DataReader because .ExecuteReader returns a DataReader
        'Good use of Using
        Using dr As New DataReader(Globals.dif)
            Dim SQL As String = "SELECT dbo.SECTION_ATTRIBUTES.SECTION_LENGTH 
    FROM dbo.SECTION_ATTRIBUTES WHERE dbo.SECTION_ATTRIBUTES.SECTION_UID = 
    @sectionUid"
            'Commands provides its own collection called Parameters
            Dim paramList As New List(Of SqlClient.SqlParameter)
            paramList.Add(New SqlClient.SqlParameter("@sectionUid",sectionUID))
            'The only argument that .ExecuteReader takes is a CommandBehavior enumeration
            '.ExecutleReader won't do anything
            dr.Execut1eReader(SQL, paramList)
            If dr.Read Then
                sectionLength = dr("SECTION_LENGTH")
            End If
        End Using
        Return sectionLength
    End Function

这可能是您的代码的替代品。您需要在文件顶部添加Imports System.Data.SqlClient

    Private Function GetSectionLength(SectionUID As Integer) As Integer
        Dim sectionLength As Integer = 0
        'Pass your connection string to the constructor of the connection
        Using cn As New SqlConnection("Your connecion string")
            'pass your sql statement and the connection directly to the constructor of the command
            Using cmd As New SqlCommand("SELECT dbo.SECTION_ATTRIBUTES.SECTION_LENGTH 
                                         FROM dbo.SECTION_ATTRIBUTES 
                                         WHERE dbo.SECTION_ATTRIBUTES.SECTION_UID = @sectionUid", cn)
                'Use the .Add method of the commands Parameters collection
                cmd.Parameters.Add("@sectionUid", SqlDbType.Int).Value = SectionUID
                'Open the connection at the last possible moment
                cn.Open()
                '.ExecuteScalar returns a single value, the first column of the first row of your query result
                sectionLength = CInt(cmd.ExecuteScalar)
            End Using 'Closes and disposes the command
        End Using 'closes and disposes the connection
        Return sectionLength
    End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    相关资源
    最近更新 更多