0

value=combobox.selectedtext のデータベースから選択するクエリがあります。

ただし、私がやりたいのは、コンボボックスのすべての値をチェックする場所の値です。

誰かがこれがどのように行われるかについてアドバイスを提供できますか?

私の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を正しく知っていないので、IIは私のコードを試さずにあなたを助けようとします:

 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 に答える