2015-06-23 32 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

謝謝喬希,它幫了我很多。其實,你在我心中。 –

相關問題