【问题标题】:Is something with my logic in this ADO.NET code? it is always return false?这个 ADO.NET 代码中的逻辑与我的逻辑有关吗?它总是返回false?
【发布时间】:2009-06-05 21:05:08
【问题描述】:

这是一个简单的函数,如果用户存在于 users 表中则返回 TRUE,否则返回 false。

由于某种原因它总是返回 false,我什至删除了 WHERE 子句,我仍然得到 false? (查询分析器中的手动检查告诉我我有很多行?)

Public Shared Function DoesUserExist(ByVal userID As Integer) As Boolean

    Dim retValue As Boolean

    retValue = False

    Using conn As New SqlConnection(GetConnectionString())

        'Dim cmd As SqlCommand = New SqlCommand("SELECT user_ID FROM users WHERE user_ID = @userID", conn)
        Dim cmd As SqlCommand = New SqlCommand("SELECT user_ID FROM users", conn)


        cmd.Parameters.Add("@userID", SqlDbType.NVarChar).Value = userID
        cmd.CommandType = CommandType.Text

        conn.Open()

        Dim reader As SqlDataReader = cmd.ExecuteReader()


        'If Not reader Is Nothing Then
        '    HttpContext.Current.Response.Write("<br>Null")
        'End If

        If reader.Read() Then

            retValue = True

        End If


        conn.Close()

        cmd.Dispose()

    End Using


    retValue = False



    Return retValue

End Function

【问题讨论】:

    标签: sql-server vb.net ado.net


    【解决方案1】:

    您在退出前将 retValue 设置为 false。这就是问题所在。

    这是正确的代码:

    ...
    
        If reader.Read() Then
    
            retValue = True
    
        Else
    
            retValue = False
    
        End If
    
    
        conn.Close()
    
        cmd.Dispose()
    
    End Using
    
    Return retValue
    
    ....
    

    【讨论】:

    • 这也是可行的。我只是指出两个代码之间的区别。
    【解决方案2】:

    可能是最后一行将“retValue”设置为 false?

    【讨论】:

      【解决方案3】:

      那是因为您在返回之前将 retValue 设置为 false。

      无论您之前将其设置为什么都没关系 - 在函数的末尾,这些行将始终为 false:

      retValue = False
      Return retValue
      

      【讨论】:

        【解决方案4】:

        代码有两个问题。

        1:您没有在查询中使用@userID 参数。因此它总是返回所有用户,唯一的结果为空的情况是根本没有用户。

        Dim cmd As SqlCommand = New SqlCommand("SELECT user_ID FROM users where user_ID = @UserID", conn)
        

        2:您在 return 语句之前再次将 retValue 变量设置为 false。因此该方法总是返回false,而不管检查数据读取器的结果如何。

        另外,您应该在数据阅读器上调用Dispose

        【讨论】:

          【解决方案5】:

          您在返回之前将 retValue 设置为 false,并且您的代码中没有其他 Return 语句,因此之前发生的事情无关紧要。去掉最后一个 retValue = False。

          你想要这样的东西:

          Public Shared Function DoesUserExist(ByVal userID As Integer) As Boolean
          
              Dim retValue As Boolean
          
              retValue = False
          
              Using conn As New SqlConnection(GetConnectionString())
                   // conditionally set retValue = true, etc.
              End Using
          
              Return retValue
          
          End Function
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-09-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-01-29
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多