SELECT Query WHERE使用checkboxlist中的多个值

时间:2015-04-12 21:01:31

标签: sql asp.net vb.net gridview checklistbox

我想知道是否可以使用checkboxlist中的多个值来过滤表中的数据? (or any other way)我有一个checkboxlist和一个gridview,当您选中框时,它确实会在gridview中显示正确的数据,但是当我尝试检查多个值时会出现问题。它似乎搜索第一个检查值,然后忽略其余的值。你认为它很简单!也许是这样。以下是我的尝试。

代码背后

Imports System.Data

Imports System.Data.SqlClient

Partial Class Default2

Inherits System.Web.UI.Page

Dim strSQL As New System.Text.StringBuilder


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack Then
            Dim i As Integer, c As Integer = 0
            Dim strParams As String = ""
            For i = 0 To Me.CheckBoxList1.Items.Count - 1
                If CheckBoxList1.Items(i).Selected Then
                    c += 1
                    If c = 1 Then
                        strParams = "(Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
                    Else
                        strParams &= " AND (Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
                    End If
                End If
            Next
            strSQL.Append("SELECT Project.*")
            strSQL.Append(" FROM Keyword INNER JOIN Project ON Keyword.ProjID = Project.ProjID")
            strSQL.Append(" WHERE" & strParams)
            FillGridView()



        End If
    End Sub


    Private Sub FillGridView()

        Dim strMyConn As String = "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\FYPMS_DB.mdf;Integrated Security=True"
        Using MyConn As New SqlClient.SqlConnection(strMyConn)
            MyConn.Open()
            Dim cmd As New SqlClient.SqlCommand(strSQL.ToString, MyConn)
            cmd.Connection = MyConn
            cmd.CommandType = CommandType.Text
            Try
                Using dr As SqlClient.SqlDataReader = cmd.ExecuteReader
                    Dim dt As New DataTable
                    dt.Load(dr)


                    Me.GridView1.DataSource = dt
                    Me.GridView1.DataBind()
                End Using
                If Me.GridView1.Visible = False Then Me.GridView1.Visible = True


            Catch ex As Exception
                Me.GridView1.Visible = False
            End Try
        End Using
    End Sub


    Protected Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim i As Integer, c As Integer = 0
        Dim strParams As String = ""
        For i = 0 To Me.CheckBoxList1.Items.Count - 1
            If CheckBoxList1.Items(i).Selected Then
                c += 1
                If c = 1 Then
                    strParams = "(Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
                Else
                    strParams &= " AND (Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
                End If
            End If
        Next
        If c <> 0 Then
            strSQL.Append("SELECT Project.*")
            strSQL.Append(" FROM Keyword INNER JOIN Project ON Keyword.ProjID = Project.ProjID")
            strSQL.Append(" WHERE" & strParams)
        End If




    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

重构此部分以创建WHERE IN语句,以便检查是否在所检查的任何项目中找到该值

<强>之前

Dim strParams As String = ""
For i = 0 To Me.CheckBoxList1.Items.Count - 1
    If CheckBoxList1.Items(i).Selected Then
        c += 1
        If c = 1 Then
            strParams = "(Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
        Else
            strParams &= " AND (Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
        End If
    End If
Next

<强>后

Dim params As StringBuilder = New StringBuilder()

For i = 0 To Me.CheckBoxList1.Items.Count - 1
    If CheckBoxList1.Items(i).Selected Then
        params.Append("'") 
        params.Append(CheckBoxList1.Items(i).Text)
        If i < Me.CheckBoxList1.Items.Count Then
            params.Append("',") // don't append a comma if it's the last item
        End If
    End If
Next
strSQL.Append("SELECT Project.* FROM Keyword INNER JOIN Project ON Keyword.ProjID = Project.ProjID WHERE Keyword.Keyword in (")
strSQL.Append(params.ToString()) // append comma delimited values that make up where in statement
strSQL.Append("')") // close where in statement
FillGridView()