对于一个列表框加载txt文件,现在我想将内容发送到sql数据库,其中有两个表m.in user,comm http://obrazki.elektroda.pl/8069892000_1409841532.png 如何添加所有项目与id用户同时记录在表中? 恩。 http://obrazki.elektroda.pl/5048367700_1409841766.png
以下方法仅在我选择一个项目时有效。
Dim con As New MySqlConnection
Dim cmd As New MySqlCommand
Try
con.ConnectionString = "Server = x; User ID = x; Password = x; Database = x;"
con.Open ()
cmd.Connection = con
cmd.CommandText = "insert into ycomm (user, comm) values ('" & ListView2.Items (0) .SubItems (0) .Text & "', '" & ListBox1.SelectedItem & "')"
cmd.ExecuteNonQuery ()
Catch ex As Exception
Finally
con.Close ()
End Try
Call showData ()
答案 0 :(得分:0)
我发现了这个但是dees现在有效,哪里有问题?
替换:
cmd.CommandText = "insert into ycomm (user, comm) values ('" _
& ListView2.Items(0).SubItems(0).Text & "', '" & ListBox1.SelectedItem & "')"
使用
cmd.CommandText = "INSERT INTO ycomm (user, comm) VALUES(@user, @comm)"
cmd.Parameters.AddWithValue("@user", ListView2.Items(0).SubItems(0).Text)
cmd.Parameters.Add("@comm", SqlDbType.VarChar)
For i As Integer = 0 To lst.Items.Count
cmd.Parameters("@comm").Value = lst.Items(i).Text
cmd.ExecuteNonQuery()
Next