如何将列表框数据作为一个大容量数据插入sql server中的表单元格中



我试图将listbox中的数据列表作为大容量数据保存到sql server,但我不知道。这是我的代码:

cmd As New SqlClient.SqlCommand("INSERT INTO tblStudentPersonalInformation(studentregistrationumber,studentname,studentgender,studentdob,studentpicture,studentntsubjectssat,studentfamilymembers,) VALUES('" & txtStudentRegistrationNumber.Text & "','" & txtStudentName.Text & "','" & cmbStudentGender.Text & "','" 
& studentdob & "','" & "',@studentpicture,'" & lbStudentSubjectsSat.Text & lbStudenttFamilyMembers.Text & "')", 
databaseconnection.cn)
cmd.Parameters.Add(New SqlClient.SqlParameter("@studentpicture", SqlDbType.Image)).Value = IO.File.ReadAllBytes(openfile.FileName)
i = cmd.ExecuteNonQuery()

受试者和家庭成员的姓名临时存储在列表框中,作为一个数据插入表格单元格。但我不知道如何将整个列表框数据存储在表单元格中。任何想法。提前感激。

我想您在问如何将列表框中的每个项转换为单个字符串?像这样:

Dim itemList() As String, listMax As Integer, result As String, delim As String = "||"
'Set delim to whatever you want in between each listbox item
listMax = ListBox1.Items.Count - 1
If listMax >= 0 Then
    ReDim itemList(listMax)
    For i = 0 To listMax
        itemList(i) = ListBox1.Items(i).ToString
    Next
    result = Join(itemList, delim)
End If

此外,请将此视为有关SQL注入的强制性警告。我看到您知道如何参数化,但仍然将用户输入的字符串直接连接到您的命令中。这个答案解释了为什么这是个坏主意。

最新更新