仅使用一个条件检索两个数据ID

时间:2014-12-03 01:33:47

标签: mysql sql asp.net vb.net for-loop

我有这个程序应该'仅使用一个条件检索两个数据ID。

这是我的代码。感谢。

Dim coys As String() = {"COY = 'A' AND COY = 'B'", "COY = 'C' AND COY = 'D'", "COY = 'E' AND COY = 'F'", "COY = 'G' AND COY = 'H'"}
    Dim sec As String() = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", _
                            "U", "V", "W", "X", "Y", "Z", "A2", "B2", "C2", "D2", "E2", "F2", "G2", "H2", "I2", "J2", "K2", "L2", _
                            "M2", "N2", "O2", "P2", "Q2", "R2", "S2", "T2", "U2", "V2", "W2", "X2", "Y2", "Z2"}



For x As Integer = 0 To coys.Length - 1
        cn.Open()
        Dim cmd As String = "SELECT a1, a2 a3, COY FROM AA WHERE " & coys(x) & " AND a5 = '" & class2.SelectedValue & "' ORDER BY COY, a2"
        Dim adp As SqlDataAdapter = New SqlDataAdapter(cmd, cn)
        adp.Fill(dt)
        GridView2.DataSource = dt
        GridView2.DataBind()
        cn.Close()
        Dim cnt As Integer = dt.Rows.Count
        For i As Integer = 0 To numsec2.SelectedValue - 1
            Dim vHeader As String = sec(i)
            If Not dt.Columns.Contains(vHeader) Then
                Dim f As New Data.DataColumn(vHeader, GetType(System.String))
                dt.Columns.Add(f)
                f.AllowDBNull = True
            End If
        Next
    Next

如果我在coys()数组中使用OR,它会显示所有数据而不确认第二个条件,如果我在coys()数组中使用AND,则它不起作用。

1 个答案:

答案 0 :(得分:1)

使用OR条件并将其放入(),这样就会符合第二个条件:

Dim coys As String() = {"(COY = 'A' OR COY = 'B')", "(COY = 'C' OR COY = 'D')", "(COY = 'E' OR COY = 'F')", "(COY = 'G' OR COY = 'H')"}