简体   繁体   English

无法从VB.NET复选框列表中构建SQL查询

[英]Trouble building an SQL Query from VB.NET checklistbox options

I am trying to create an sql query from options selected in a checkListBox. 我试图从在checkListBox中选择的选项创建一个sql查询。 The user will select all of the modules they want (in the checklistbox) to include data from, it will then build the query to collect this data. 用户将选择他们想要的所有模块(在复选框中)以包括数据,然后将构建查询以收集此数据。 They will also enter a range for a rating value that will be included in the query. 他们还将输入将包含在查询中的额定值范围。 I am very new to using sql so I am struggling to understand what operator is missing from the final query. 我对使用sql非常陌生,因此我努力了解最终查询中缺少什么运算符。

This is what I have at the moment: 这是我目前所拥有的:

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim h As Integer
    Dim queryString As String
    Dim moduleArray(6) As String
    Dim counter As Integer = 0
    Dim provider As String
    Dim database As String
    Dim connString As String
    Dim moduleLen As Integer = 0
    Dim moduleString As String = ""
    Dim sqlquery As New OleDb.OleDbCommand

    provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
    Change the following to your access database location
    database = "C:\Users\mello_000\OneDrive\Google Drive\Computing\Exampapergenerator\users.accdb"
    connString = provider & database
    Dim myConnection As OleDbConnection = New OleDbConnection(connString)

    myConnection.Open()
    sqlquery.Connection = myConnection
    For h = 0 To h = 6
        For Each item As String In Me.CheckedListBox1.CheckedItems
            moduleArray(moduleLen) = item
            If moduleArray(moduleLen) = "" Then
            Else
                moduleLen = moduleLen + 1
            End If
        Next
    Next

    For i = 0 To moduleLen
        If i = 0 Then
            moduleString = "'" & moduleArray(i) & "'"
        ElseIf i > 0 Then
            moduleString = moduleString & "'OR'" + "'" & moduleArray(i) & "'"
        End If
    Next


    queryString = ("SELECT QText FROM Question WHERE QModule = '" & moduleString & "' AND QRating BETWEEN '" & TextBox1.Text() & "'AND'" & TextBox2.Text())
    sqlquery.CommandText = queryString
    sqlquery.ExecuteNonQuery()



End Sub

However I am getting the output to be: "SELECT QText FROM Question WHERE QModule = ''C1''OR''C2'' AND QRating BETWEEN '1'AND'2" and an error: 但是,我得到的输出是: "SELECT QText FROM Question WHERE QModule = ''C1''OR''C2'' AND QRating BETWEEN '1'AND'2"并在"SELECT QText FROM Question WHERE QModule = ''C1''OR''C2'' AND QRating BETWEEN '1'AND'2""SELECT QText FROM Question WHERE QModule = ''C1''OR''C2'' AND QRating BETWEEN '1'AND'2"进行"SELECT QText FROM Question WHERE QModule = ''C1''OR''C2'' AND QRating BETWEEN '1'AND'2"和一个错误:

Syntax error (missing operator) in query expression 'QModule = ''C1''OR''C2'' AND QRating BETWEEN '1'AND'2'.

Also, what would be the best way of outputting all of the returned data in a numbered list, in a form that would be printable? 另外,以可打印的形式输出编号列表中所有返回数据的最佳方法是什么?

Why are you doing this For h = 0 To h = 6 instead of just For h = 0 To 6 ? 为什么要For h = 0 To h = 6而不是仅仅For h = 0 To 6

You don't need single quotes around "'OR'" just use " OR " . 您不需要在"'OR'"周围加上单引号,只需使用" OR "

And your SQL syntax is wrong. 而且您的SQL语法错误。 This QModule = ''C1''OR''C2'' either needs to be QModule = 'C1' OR QModule = 'C2' or a better way QModule IN ('C1','C2') 这个QModule = ''C1''OR''C2''或者是QModule = 'C1' OR QModule = 'C2'或更好的方法QModule IN ('C1','C2')

Assuming QRating is numeric, you don't need single quotes. 假设QRating是数字形式,则不需要单引号。 This QRating BETWEEN '1'AND'2' should be QRating BETWEEN 1 AND 2. QRating BETWEEN '1'AND'2' QRating BETWEEN 1 AND 2. QRating BETWEEN '1'AND'2'应该在QRating BETWEEN 1 AND 2.

Also you should look into using SQL parameters so you don't have to worry about quotes or escaping them if you have quotes in your data. 另外,您应该研究使用SQL参数,这样就不必担心引号或如果数据中包含引号就可以转义它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM