使用CheckedListBox项显示关联数据

时间:2017-02-27 09:32:55

标签: database vb.net checkedlistbox

  

我有一个代码,用于显示MS Access数据库中的项目   checkedlistbox

  Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim con As New OleDb.OleDbConnection
    con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\rose&mike\Desktop\DbSysDel3\salondbaccess.accdb"
    Dim cmd As OleDbCommand
    Dim sql As String = "SELECT Service_Name FROM [Service_Types]"
    con.Open()
    cmd = New OleDb.OleDbCommand(sql, con)
    Dim dr As OleDbDataReader = cmd.ExecuteReader
    If dr.HasRows Then
        While dr.Read
            chcklstbx1.Items.Add(dr(0))
        End While
    End If
    con.Close()
  End Sub

我需要做的是在选中项目时显示关联的Service_Fee并将其转换为String。任何建议都受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

Dim selec As String
    Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\rose&mike\Desktop\DbSysDel3\salondbaccess.accdb")
    Dim dt2 As New DataTable
    selec = ""
    con.Open()
    For Each incheck In chcklstbx1.CheckedIndices
        Dim valName As String
        valName = chcklstbx1.Items.Item(incheck).ToString
        Dim sqlstr2 As String = "SELECT Service_Fee FROM Service_Types WHERE [Service_Name] = '" & valName & "'"
        Dim cmd As New OleDbCommand(sqlstr2, con)
        Dim sum As Double = 0
        Dim rdr As OleDbDataReader = cmd.ExecuteReader
        If rdr.HasRows Then
            While rdr.Read
                selec += "P" + rdr("Service_Fee").ToString & ControlChars.NewLine
            End While
        End If
        lblFees.Text = selec
        rdr.Close()
    Next
    con.Close()