OleDbException未处理,WHERE子句中的语法错误?

时间:2014-03-02 15:49:59

标签: vb.net

遇到持久性错误。我的表单允许用户查看特定组中的元素列表。组号的输入是一个名为groupbox的组合框,输出是一个名为ElementResults的组合框。我收到了错误:GroupSearch.ExecuteNonQuery()

Imports System.Data.OleDb
Public Class ElementsSearch
Public Shared connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data        Source=C:\Documents and Settings\A\My Documents\Visual Studio 2010\Projects\A2\Element.accdb;Persist Security Info=False;"
Public Shared ElementsTable

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

    Dim Search As New OleDbConnection(connectionString)
    Search.Open()

    Dim SearchCriteria As String
    SearchCriteria = Groupbox.Text
    Dim query As String = "SELECT * From ElementsTable WHERE Group='" & SearchCriteria & "'"
    Dim GroupSearch As New OleDbCommand(query, Search)
    GroupSearch.ExecuteNonQuery()
    Dim reader As OleDbDataReader = GroupSearch.ExecuteReader()
    ElementResults.Text = Convert.ToString(reader("Name"))

End Sub
End Class

2 个答案:

答案 0 :(得分:2)

您定义字段group,这是一个保留关键字。

请尝试使用[group],例如:

SELECT * From ElementsTable WHERE [Group]='aaa'

答案 1 :(得分:1)

为了避免您遇到的所有错误,请考虑将代码更改为以下内容:

Using connection As New OleDbConnection(connectionString)
    connection.Open()
    Using command As New OleDbCommand("SELECT * From ElementsTable WHERE [Group]=@Group", connection)
        command.Parameters.AddWithValue("@Group", SearchCriteria)
        Using reader As OleDbDataReader = command.ExecuteReader()
            Do While reader.Read()
                ElementResults.Text = reader.GetString("Name")
            Loop
        End Using
    End Using
End Using