vb.net数据库查询和复选框,正确的方法吗?

时间:2014-12-30 10:56:45

标签: vb.net

我是Vb.net和Sql Server的新手(不要太苛刻)。任何人都可以检查我的代码,我觉得这是错误的方法。目的是仅在单击复选框并显示该查询时显示查询。 非常感谢你。

 Private Sub btnFilter_Click(sender As Object, e As EventArgs) Handles btnFilter.Click

    CheckValue = 0 'set to value 0 
    'check if any checkbox is checked.
    For Each C As Control In Me.Controls
        If C.GetType Is GetType(CheckBox) Then
            Dim cb As CheckBox = DirectCast(C, CheckBox)
            If cb.Checked Then
                CheckValue = CheckValue + 1 'only works if value is above 1
            End If
        End If
    Next


    If CheckValue <> 0 Then   'only if check box is checked then rest will run

        Try
            If chkboxId.Checked = True Then 'assign value to checkbox
                chkboxIdstr = "userid"
            Else
                chkboxIdstr = ""
            End If

            If chkboxName.Checked = True Then
                chkboxNamestr = "username"
            Else
                chkboxNamestr = ""
            End If

            'to insert comma, only if both are true.
            If chkboxId.Checked = True And chkboxName.Checked = True Then
                SQLcomma = " , "
            Else
                SQLcomma = ""
            End If

            'so if both are checked we get a comma
            SQL2 = chkboxIdstr + " " + SQLcomma + " " + chkboxNamestr
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Try 'sql connect and display
            sqlCon.Open()
            SQL = "SELECT " + SQL2 + " FROM user_info"

            Dim dadapter As New SqlDataAdapter(SQL, sqlCon)
            Dim dset As New DataSet
            sqlCon.Close()
            dadapter.Fill(dset)
            'fill datagrid
            dgv.DataSource = dset.Tables(0)

        Catch ex As Exception
            MsgBox(ex.Message)
            sqlCon.Close()
        End Try
    Else
        MsgBox("Plesase do check any box to filter ")
        Exit Sub

    End If
End Sub

- 我通过做小应用程序(跟踪和错误)来学习,主要是谷歌搜索解决方案。

2 个答案:

答案 0 :(得分:1)

我认为您不需要所有代码。只需逐个测试您的复选框,并逐步构建要添加到SELECT查询的列列表....

Dim selectColumns As String
Try

    If chkboxId.Checked = True Then 
        selectColumns = "userid,"
    End If

    If chkboxName.Checked = True Then
        selectColumns &= "username,"
    End If

    ' Other columns???? follow the same pattern

    ' If we have something in selectColumns then we could execute the query
    ' but before we trim away the last comma
    If selectColumns.Length > 0 Then 
       selectColumns = selectColumn.TrimEnd(",")

       sqlCon.Open()
       SQL = "SELECT " & selectColumns & " FROM user_info"
       .....
    Else
       ' no data in selectColumns? Message for you user
       MsgBox("Plesase do check any box to filter ")
       Exit Sub
    End If    
Catch ex As Exception
    MsgBox(ex.Message)
    sqlCon.Close()
End Try

答案 1 :(得分:1)

试试这个:

Private Sub btnFilter_Click(sender As Object, e As EventArgs) Handles btnFilter.Click

    If chkboxId.Checked OrElse chkboxName.Checked Then
        SQL2 = String.Empty
        If chkboxId.Checked Then
            SQL2 = SQL2 + "userid"
        EndIf

        If chkboxId.Checked AndAlso chkboxName.Checked Then
            SQL2 = SQL2 + " , "
        End If

        If chkboxName.Checked Then
            SQL2 = SQL2 + "username"
        End If

        Try 'sql connect and display
            sqlCon.Open()
            SQL = "SELECT " + SQL2 + " FROM user_info"

            Dim dset As New DataSet
            Using dadapter As New SqlDataAdapter(SQL, sqlCon)
                dadapter.Fill(dset)
            End Using

            'fill datagrid
            dgv.DataSource = dset.Tables(0)

          Catch ex As Exception
             MsgBox(ex.Message)
          Finally
             sqlCon.Close()
          End Try
    Else
        MsgBox("Please do check any box to filter ")
    End If

End Sub