根据CheckedListBox中的多个检查项检索数据库中的数据

时间:2014-04-03 08:04:20

标签: vb.net

如何根据CheckedListBox中的多个检查项从数据库中检索数据?指导我!!!

我目前正在做一个关于根据CheckedListBox中的多个检查项检索表中数据的教程。现在我只能通过1个检查项目从表中检索数据。如何通过倍数检查项来检索它?

  1. checklistbox1
  2. checklistbox2
  3. 教程将:首先将所有相关数据加载到checklistbox1作为项目​​,并且用户可以去检查列出的项目,一旦用户检查了详细项目,checklistbox2现在将从另一个表格查询数据[field] =来自checklistbox1的检查项目。

    将事件加载为类

    Public Sub Startload()
            Dim connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & aaa & "';Persist Security Info=False;")
            Dim dt1 As New DataTable
            Dim sqlstr As String = "SELECT * FROM tbl"
            Dim command As New OleDbCommand(sqlstr, connection)
            Dim adpt As New OleDbDataAdapter(command)
    
            adpt.SelectCommand = command
            adpt.Fill(dt1)
            CheckedListBox1.DisplayMember = "name"
            CheckedListBox1.ValueMember = "ID"
            CheckedListBox1.DataSource = dt1
        End Sub
    
    选中

    更改执行checkedload()

     Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
            Label1.Text = CheckedListBox1.SelectedValue
            checkedload()
        End Sub
    

    来自checklistbox1(checkedload)的检查项目将执行并检索数据并显示在checklistbox2中

    Private Sub checkedload()
            Dim x As String = Label1.Text
            Dim connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & aaa & "';Persist Security Info=False;")
            Dim dt2 As New DataTable
            Dim sqlstr2 As String = "SELECT * FROM tbl2 WHERE [name]='" & x & "'"
            Dim command2 As New OleDbCommand(sqlstr2, connection)
            Dim adpt2 As New OleDbDataAdapter(command2)
    
            adpt2.SelectCommand = command2
            adpt2.Fill(dt2)
            CheckedListBox2.DisplayMember = "namex"
            CheckedListBox2.ValueMember = "ID"
            CheckedListBox2.DataSource = dt2
        End Sub
    

2 个答案:

答案 0 :(得分:1)

自我解决

Dim i As Integer
        Dim connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & aaa & "';Persist Security Info=False;")
        Dim dt2 As New DataTable
        For i = 0 To CheckedListBox1.Items.Count - 1 Step i + 1
            If CheckedListBox1.GetItemCheckState(i) = CheckState.Checked Then
                Dim xx As String = (CType(CheckedListBox1.Items(i), DataRowView))("name")
                Dim sqlstr2 As String = "SELECT * FROM tbl2 WHERE [name]='" & xx & "'"
                Dim command2 As New OleDbCommand(sqlstr2, connection)
                Dim adpt2 As New OleDbDataAdapter(command2)
                adpt2.SelectCommand = command2
                adpt2.Fill(dt2)
                CheckedListBox2.DisplayMember = "namex"
                CheckedListBox2.ValueMember = "ID"
                CheckedListBox2.DataSource = dt2
            End If
        Next

答案 1 :(得分:-1)

Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
 Dim i as Integer=0
 For Each li as ListItem in CheckedListBox1.Items
      If li.Selected=True Then
         Dim lab As New Label
         lab.ID="lab" & i
         lab.Text=li.SelectedItem.Value
         i += 1
      End If
 Next
 hiddenfield1.Value=i    'hidden field 
 checkedload()
End Sub

Private Sub checkedload()
  For i as Integer = o To hiddenField1.Value 
    Dim x As String = Request.Form("lab" & i)
    Dim connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & aaa & "';Persist Security Info=False;")
    Dim dt2 As New DataTable
    Dim sqlstr2 As String = "SELECT * FROM tbl2 WHERE [name]='" & x & "'"
    Dim command2 As New OleDbCommand(sqlstr2, connection)
    Dim adpt2 As New OleDbDataAdapter(command2)

    adpt2.SelectCommand = command2
    adpt2.Fill(dt2)
    CheckedListBox2.DisplayMember = "namex"
    CheckedListBox2.ValueMember = "ID"
    CheckedListBox2.DataSource = dt2
  Next
End Sub

无论如何我还没有检查过它。但它可以帮到你