【问题标题】:Select statement An expression of non-boolean type specified in a context where a condition is expected, near ','Select 语句 在预期条件的上下文中指定的非布尔类型的表达式,靠近 ','
【发布时间】:2013-07-09 13:50:28
【问题描述】:

我正在尝试使用自动完成 ajax 控件搜索姓氏和名字。但我收到错误 An expression of non-boolean type specified in an context specified in an context where a condition is expected, near ','。知道我做错了什么吗? Words(0) 行出现错误。 谢谢!

Public Function GetCompletionList(prefixText As String, count As Integer, ByVal contextKey As String) As String()
    Try
        Dim words As String() = prefixText.Split(New Char() {","c})
        Dim Con As SqlConnection
        Dim cmd As SqlCommand
        Con = New SqlConnection
        Dim test As String
        test = contextKey
        Con.ConnectionString = ""
        Con.Open()

        cmd = New SqlCommand
        cmd.Connection = Con
        cmd.CommandText = "SELECT NPI, [Entity Type Code], [Provider Last Name (Legal Name)], [Provider First Name],[Provider First Line Business Mailing Address], [Provider Business Mailing Address City Name], [Provider Business Mailing Address State Name], [Provider Business Mailing Address Postal Code] FROM NPIData WHERE ([Provider Business Mailing Address State Name] = @State) AND ('" & words(0) & "','" & words(1) & "' LIKE N'%' + @Provider + N'%') ORDER BY [Provider First Name]"
        cmd.Parameters.AddWithValue("@Provider", prefixText)
        cmd.Parameters.AddWithValue("@State", contextKey)
        Dim customers As List(Of String) = New List(Of String)
        Dim reader As SqlDataReader = cmd.ExecuteReader()


        While reader.Read
            customers.Add(reader("Provider Last Name (Legal Name)").ToString + ", " + reader("Provider First Name").ToString + "   " + reader("Provider First Line Business Mailing Address").ToString + "  " + reader("Provider Business Mailing Address City Name").ToString + ", " + reader("Provider Business Mailing Address State Name").ToString + "  " + reader("Provider Business Mailing Address Postal Code").ToString + "  " + reader("NPI").ToString)

        End While


        Con.Close()

        Return customers.ToArray
    Catch ex As Exception

    End Try

End Function

【问题讨论】:

  • 您的代码会产生这样的查询:SELECT ... FROM NPIData WHERE ([Provider Business Mailing Address State Name] = @State) AND ('word_1','word_2' LIKE N'%' + @Provider + N'%') ORDER BY [Provider First Name](为简洁起见省略了选择列表)查看带有LIKE 的部分。那是无效的 SQL。您实际上想在这里实现什么目标?
  • 你能打印分配给 cmd.CommandText 的文本吗?
  • 我刚刚尝试再次运行它并且索引超出了数组的范围。
  • 我正在尝试搜索姓氏、名字。不知何故,我需要拆分“,”,因此为什么我有 Dim words As String() = prefixText.Split(New Char() {","c})

标签: asp.net sql vb.net


【解决方案1】:

在你说你失败的那一行

('" & words(0) & "','" & words(1) & "' LIKE N'%' + @Provider + N'%')

如果 word(0) 包含 txt1 而 worda (1) 包含 txt2,则计算结果为

'txt1','txt2'喜欢...

您不能将逗号分隔的字符串列表与 LIKE 进行比较。

不知道你是不是打算把words(0)和words(1)拼接起来得到

'txt1txt2' LIKE ...

这需要

('" & words(0) & words(1) & "' LIKE N'%' + @Provider + N'%')

或进行单独比较,这需要

('" & words(0) & "' LIKE N'%' + @Provider + N'%' OR ' & words(1) & "' LIKE N'%' + @Provider + N'%')

【讨论】:

  • 我认为我的答案中的单引号和双引号也有问题,但希望它足以说明问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-23
  • 1970-01-01
  • 2011-09-22
  • 2013-02-11
  • 1970-01-01
相关资源
最近更新 更多