我不知道 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")
我希望我有所帮助