2015-06-23 33 views
0

我试图将列表框中的数据列表作为批量数据保存到sql server中,但我不知道。 这是我的代码:如何在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() 

的科目名称坐着和家人暂时存储在表盒中作为一个数据的表格单元格中插入。 但我不知道如何将整个列表框数据存储在表格单元格中。有任何想法吗。提前欣赏。

+0

你想保存listbox的所有值吗? – hdkhardik

回答

0

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

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注入的强制性警告。我看到你知道如何参数化,但仍然将用户输入的字符串直接连接到你的命令中。 This answer解释了这是一个坏主意。

+0

谢谢乔希,它帮了我很多。其实,你在我心中。 –

相关问题