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中放入什么。如有任何帮助,我们将不胜感激。提前感谢
试试这个代码。这应该以逗号分隔的字符串形式返回选中列表框中的选中项。
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
use this code, is simple but worked for me:
on your click event add:
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)
通过这种方式,您可以获得以逗号分隔的字符串形式的值列表。