查询表达式 vb.net 中缺少运算符

Posted

技术标签:

【中文标题】查询表达式 vb.net 中缺少运算符【英文标题】:missing operator in query expression vb.net 【发布时间】:2011-12-09 19:18:33 【问题描述】:

我从下面的查询中收到以下错误:

查询表达式 'LTC_FBS.DBS_NM = LTC_FBS_EMP.EMP_ID 中的语法错误(缺少运算符) 在 LTC_FBS.FBS_REV = LTC_FBS_REV.REV_I' 上的 INNER JOIN LTC_FBS_REV。

     Dim sSQL As String = "SELECT FBS_ID, CLM_NUM, EMP_NM, REV_NM, DATE_SUBMIT FROM 
    LTC_FBS f INNER JOIN LTC_FBS_EMP e on f.DBS_NM = e.EMP_ID INNER JOIN LTC_FBS_REV r on 
   f.FBS_REV = r.REV_ID WHERE "

    If OptClaimNo.Checked = True Then 
        sSQL &= "CLM_NUM Like ('" & txtClaimNo.Text & "%')"

这是所有代码:

        Private sConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=O:\UA\FINANCE\LTC\MIS\Department\Databases\FBS.accdb"
Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
    Me.lstResults.DataSource = Nothing
    Me.lstResults.Refresh()






    Dim OptControlArray() As Control = OptClaimNo, OptDate, OptCustodian


    Dim sSQL As String = "SELECT FBS_ID, CLM_NUM, EMP_NM, REV_NM, DATE_SUBMIT FROM LTC_FBS INNER JOIN LTC_FBS_EMP on LTC_FBS.DBS_NM = LTC_FBS_EMP.EMP_ID INNER JOIN LTC_FBS_REV on LTC_FBS.FBS_REV = LTC_FBS_REV.REV_ID WHERE "
    sSQL &= "CLM_NUM Like ('" & txtClaimNo.Text & "%')"
    ' If OptClaimNo.Checked = True Then
    'sSQL &= "CLM_NUM Like ('" & txtClaimNo.Text & "%')"

    'sSQL &= "Last Like ('" & txtLastName.Text & "%') AND First Like ('" & txtFirstName.Text & "%')"
    ' ElseIf OptCustodian.Checked = True Then
    ' sSQL &= "DBS_NM Like ('" & cboCustodian.SelectedValue & "%')"
    'ElseIf OptDate.Checked = True Then
    ' sSQL &= "DATE_SUBMIT = ('" & txtDateRec.Text & "')"
    ' End If

    Dim conn As New System.Data.OleDb.OleDbConnection(sConnectionString)
    conn.Open()

    Dim dat As OleDbDataAdapter = New OleDbDataAdapter(sSQL, conn)
    Dim dt As New DataTable()

    dat.Fill(dt)
    dat.Dispose()

    If dt.Rows.Count <> 0 Then
        lstResults.DataSource = dt
        Me.lstResults.Columns("FBS_ID").Visible = False
        Me.lstResults.Columns("REV_NM").Width = 125
        Me.lstResults.Columns("EMP_NM").Width = 100
        Me.lstResults.Columns("DATE_SUBMIT").Width = 150

    Else
        MsgBox("No records were found.")
    End If

    dt.Dispose()
    conn.Close()

   End Sub
   End Class

【问题讨论】:

select 语句和你的错误信息不匹配。您能否使用您指定的语句验证错误是否发生,如果是,请更新错误消息? 你的意思是 rev_i 在哪里被截断?它在运行它的 dat.fill 处抛出错误。这基本上是我在表单后面的唯一代码。 您的代码容易受到 sql 注入的攻击。这是一个大问题。请修复它。 【参考方案1】:

问题在于两个内连接。您需要将第一个用括号括起来才能正确处理它。

变化:

Dim sSQL As String = "SELECT FBS_ID, CLM_NUM, EMP_NM, REV_NM, DATE_SUBMIT FROM LTC_FBS INNER JOIN LTC_FBS_EMP on LTC_FBS.DBS_NM = LTC_FBS_EMP.EMP_ID INNER JOIN LTC_FBS_REV on LTC_FBS.FBS_REV = LTC_FBS_REV.REV_ID WHERE "

到:

Dim sSQL As String = "SELECT FBS_ID, CLM_NUM, EMP_NM, REV_NM, DATE_SUBMIT FROM (LTC_FBS INNER JOIN LTC_FBS_EMP on LTC_FBS.DBS_NM = LTC_FBS_EMP.EMP_ID) INNER JOIN LTC_FBS_REV on LTC_FBS.FBS_REV = LTC_FBS_REV.REV_ID WHERE "

【讨论】:

以上是关于查询表达式 vb.net 中缺少运算符的主要内容,如果未能解决你的问题,请参考以下文章

查询表达式中的语法错误(缺少运算符)“

访问表单 - 查询表达式中的语法错误(缺少运算符)

在 MS Access 中的查询表达式中出现语法错误(缺少运算符)

OleDb 进入 Access 数据库的查询表达式中缺少运算符

查询表达式中的语法错误(缺少运算符)

VB .NET OleDbCommand 在 SQL 语句末尾返回缺少分号 (;)