0

我有一个从 db 中选择 value=combobox.selectedtext 的查询。

但是,我想做的是让 where 值检查组合框的所有值。

有人可以就如何做到这一点提供建议吗?

我的 sql 查询现在是这样的:

Dim sqlOpServ As String = ("SELECT DISTINCT col1, col2 from table1, table2 where test=test1 AND value= '" & combobox1.SelectedItem & "' ORDER BY col1 ASC")

谢谢

4

2 回答 2

1

我不知道 VS,所以我会在不尝试我的代码的情况下尝试帮助你:

 Dim sWhere as string=""
 For i = 0 To ComboBox1.Items.Count - 1
        sSelect = sSelect & " AND value='" & ComboBox1.Items(i) & "' "
 Next

 Dim sqlOpServ As String = ("SELECT DISTINCT col1, col2 from table1, table2 where test=test1 " & sWhere & " ORDER BY col1 ASC")

评论后:

   Dim sWhere As String = ""

    For i = 0 To ComboBox1.Items.Count - 1
        If i = 0 Then
            sWhere = " AND value='" & ComboBox1.Items(i).ToString & "' "
        Else
            sWhere = sWhere & " OR value='" & ComboBox1.Items(i).ToString & "' "
        End If

    Next

    Dim sqlOpServ As String = _
        ("SELECT DISTINCT col1, col2 from table1, table2 where test=test1 " & sWhere & " ORDER BY col1 ASC")

如果您使用数据表填充组合框

    Dim table As DataTable = DirectCast(Me.ComboBox1.DataSource, DataTable)
    Dim sWhere As String = ""

    For i = 0 To ComboBox1.Items.Count - 1
        Dim displayItem As String = table.Rows(i)(ComboBox1.DisplayMember).ToString()
        ' Dim valueItem As String = table.Rows(i)(ComboBox1.ValueMember).ToString() 'if you need at some point the value you can use this

        If i = 0 Then
            sWhere = " AND value='" & displayItem & "' "
        Else
            sWhere = sWhere & " OR value='" & displayItem & "' "
        End If

    Next

    Dim sqlOpServ As String = _
        ("SELECT DISTINCT col1, col2 from table1, table2 where test=test1 " & sWhere & " ORDER BY col1 ASC")

我希望我有所帮助

于 2012-12-18T10:36:15.660 回答
0

2 种方法

1)您可以制作所有组合框项目值并用特殊字符分隔,您可以拆分存储过程中的所有值并获得结果

2) 您可以创建一个以“或”分隔的字符串,即 value=22 或 value=25,然后您可以执行这些值。

于 2012-12-18T10:32:59.200 回答