将Checked项从CheckedListBox转换为String然后将其存储到MySQL数据库

时间:2015-04-08 07:28:32

标签: php mysql vb.net

新手在VB中。有没有简单或简单的方法将我的checkedlistbox中的5个已检查项目转换为字符串,以便我可以将它们保存在mySQL数据库中?还是更简单的东西?到目前为止,我的代码就像这样。

Try
        cnn.Open()
        com = New MySqlCommand("SELECT id FROM columns WHERE cname ='" & TextBox1.Text & "'", cnn)
        cdr = com.ExecuteReader
        If cdr.HasRows Then
            cdr.Close()
            cnn.Close()
            MsgBox("This Table has already been created!")
            TextBox1.Focus()
            Exit Sub
        End If

        cdr.Close()
        com = New MySqlCommand("INSERT into column1, column2, column3, column4, column5, cname) VALUES('" ??? "', '" & TextBox1.Text &"')", cnn)
        com.ExecuteNonQuery()
        cnn.Close()
        MsgBox("Attendance Table has been created.")
    Catch ex As Exception
        If Not cnn.State = ConnectionState.Closed Then
            cnn.Close()
        End If
        MsgBox(ex.ToString)
    End Try

我现在卡在INSERT MySQLCommand之后放入VALUES的内容。任何帮助,将不胜感激。提前谢谢

2 个答案:

答案 0 :(得分:0)

试试这段代码。这应该将选中的项目作为逗号分隔的字符串返回到已检查的列表框中。


Private Function getChkdBoxString() As String
    Dim myChkdBoxes = CheckedListBox.CheckedItems 'rename CheckedListBox to the name of the checked list box that you have used
    Dim chkBoxString As String = ""
    For Each chkBox In myChkdBoxes
        chkBoxString = chkBox.ToString & "," & chkBoxString
    Next
    Return chkBoxString
End Sub

答案 1 :(得分:0)

使用此代码,很简单,但对我有用:

在您的点击事件上添加:

    Dim mycheckedlist = CheckedListBox1.CheckedItems
    Dim checkedstr As String = ""

    For Each chkBox In mycheckedlist
    checkedstr = chkBox.ToString & "," & checkedstr


    Next

    (here you add your code to insert the checkstr 
    variable as a string into your database)

这样,您将以逗号分隔的值列表作为字符串。