sql查询返回整列数据

时间:2018-03-19 14:33:42

标签: vb.net ms-access

我有一个VB.net程序,它将一组变量解析为一个用于与Access数据库通信的子程序。 查询字符串显示为:

SELECT ID FROM cbRooms WHERE @condition

@condition的代码值为

.Parameters.AddWithValue("@condition", dbCondition)

dbCondition的位置,在这种情况下:

H_Block=True

当我直接在Access中运行此查询时,我得到了预期的数据,这是一组数字,在Access中有一个勾选框

但是,当我在Visual Studio中运行相同的代码时,它只返回整个列,无论该复选框是否为真

我用VB编写了一段时间已经有一段时间了,所以我很生疏,所以这个问题很可能是我的一个简单的疏忽

以下是sql访问函数的代码:

Public Function sqlSelect(ByVal dbCol As String, ByVal dbTable As String, ByVal dbCondition As String)


    'Creating the sqlCmd string to sent as an SQL request to the Database
    Dim sqlCmd As String
    'Creating a new connection to the database
    Dim conn As New OleDb.OleDbConnection

    'Setting the value for the sqlCmd string, with several "@" parameters
    sqlCmd = "SELECT " & dbCol & " FROM " & [dbTable] & " WHERE @condition;"
    'Running the Connect Sub routine to interact with the database
    Connect(conn)

    Using conn
        Using dbEvent As New OleDb.OleDbCommand
            With dbEvent
                'sets the connection used by the current instance of OleDB usng the conn string
                .Connection = conn
                'Sets how the .CommandType is interpreted
                .CommandType = CommandType.Text
                'Sets the sqlCmd string that will be sent
                .CommandText = sqlCmd
                'Setting the "@" parameters in the sqlCmd string using values parsed into the sub routine
                .Parameters.AddWithValue("@condition", dbCondition)
            End With

            Try
                'Opening connection to the database
                conn.Open()
                'Creating the _sqlRead Reader used to read the data coming from the database
                Dim sqlReader As OleDb.OleDbDataReader
                sqlReader = dbEvent.ExecuteReader()

                'Creates a list that will store the values returned to the Search.vb class
                Dim returnVals As New List(Of Integer)

                Do While sqlReader.Read = True
                    returnVals.Add(sqlReader(dbCol))
                Loop

                Return returnVals

            Catch ex As Exception
                'Opens a message box showing the current error
                MessageBox.Show(ex.Message.ToString(), "Error Message")
            End Try
        End Using
    End Using
End Function

以下是调用AccessSQL.vb的组合框的代码:

Public Sub cbBlock_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbBlock.SelectedIndexChanged
    Select Case cbBlock.Text
        Case "H Block"
            cbRoom.Items.Clear()
            Dim listOfRoomsInBlock As New List(Of Integer)
            listOfRoomsInBlock = sqlSelect("ID", "cbRooms", "H_Block=True")
            For x = 0 To listOfRoomsInBlock.Count - 1
                cbRoom.Items.Add(listOfRoomsInBlock(x))
            Next
        Case "Tech Block"
            cbRoom.Items.Clear()
            Dim listOfRoomsInBlock As New List(Of Integer)
            listOfRoomsInBlock = sqlSelect("ID", "cbRooms", "T_Block=True")
            For x = 0 To listOfRoomsInBlock.Count - 1
                cbRoom.Items.Add(listOfRoomsInBlock(x))
            Next

    End Select
End Sub

感谢您的帮助!

编辑: SQL在VB中运行时返回This到我试图填充的组合框中 底部的1 - 13值是我所期待的,而14 - 30则不是。我的想法是我的参数化实际上从未使用我在上面设置的条件。

然而,当我在Access中运行相同的expected query时,我得到了正确的values returned

0 个答案:

没有答案