首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将选中项从CheckedListBox转换为字符串,然后将其存储到MySQL数据库

将选中项从CheckedListBox转换为字符串,然后将其存储到MySQL数据库
EN

Stack Overflow用户
提问于 2015-04-08 15:28:32
回答 2查看 383关注 0票数 0

这里是VB的新手。有没有什么简单或简单的方法可以将我的选中列表框中的5个选中项转换为字符串,以便我可以将它们保存在mySQL数据库中?或者更简单的东西?到目前为止我的代码是这样的。

代码语言:javascript
运行
复制
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

我现在正纠结于在插入MySQLCommand之后输入什么值。任何帮助都将不胜感激。提前谢谢你

EN

回答 2

Stack Overflow用户

发布于 2015-04-08 15:40:07

试试这段代码。这将以逗号分隔的字符串形式返回选中列表框中的选中项目。

代码语言:javascript
运行
复制
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

票数 0
EN

Stack Overflow用户

发布于 2020-01-20 10:22:32

使用下面的代码,很简单,但对我来说很有效:

单击event add:

代码语言:javascript
运行
复制
    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)

通过这种方式,您可以获得以逗号分隔的字符串形式的值列表。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29508494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档