如何将多个项目(所有项目)从列表框插入数据库(ms.access) [英] How to insert multiple items(all item) from listbox to database(ms.access)

查看:116
本文介绍了如何将多个项目(所有项目)从列表框插入数据库(ms.access)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我是编程初学者,我只从网上学习书籍和搜索。

我是目前制作一个简单的程序来列出我文件夹中所有文件的名称和路径。

我得到一个小问题,我很困惑,找不到错误。

问题我收到的错误是索引超出了数组的范围。但是当我看到数据库已经成功填充但只有100行/项目时,我的问题是如何在列表框中将多个项目(超过1000个项目)插入数据库(ms.access)?谢谢:)

抱歉我的英文不好:)



我尝试了什么:



hi all,

I am a beginner in programming, I learn only with books and searches from internet.
I'm currently making a simple program to listing name and path of all files in my folder.
I get a small problem that I am confused to find where the error.
the problem is I getting an error like "Index was outside the bounds of the array." But when I see the database has been successful filled but only 100 lines / items
my question is how to insert multiple items(more than 1000 items) in listbox to database(ms.access)?. thanks :)
sorry for my bad english :)

What I have tried:

Private Sub itemsimport()
if conn.State = ConnectionState.Closed Then conn.Open()
        Dim sql As String = "INSERT INTO data_lagu(title, singer, kategori, novocal, path_lagu) VALUES (@TITLE, @SINGER, @KATEGORI, @NOVOCAL, @PATH)"
        For Each myitem As String In ListBox2.SelectedItems
            Dim cmd As New OleDbCommand(sql, conn)
            Dim lineArray() As String = myitem.Split("#")
            cmd.Parameters.AddWithValue("@TITLE", lineArray(0))
            cmd.Parameters.AddWithValue("@SINGER", lineArray(1))
            cmd.Parameters.AddWithValue("@KATEGORI", lineArray(2))
            cmd.Parameters.AddWithValue("@NOVOCAL", lineArray(3).Replace(".MPG", ""))
            cmd.Parameters.AddWithValue("@PATH", myitem)
            cmd.ExecuteNonQuery()
        Next
        MsgBox("success")
        conn.Close()
        loaddata()
End Sub

推荐答案

这与数据有关。您要分割的其中一行不包含所有4个项目。查看ListBox中的信息或查看填充ListBox的位置。



您可以在使用之前检查lineArray的上限,然后跳转到ListBox中的下一个项目。或者考虑使用不同的控件来显示您的数据,例如DataGridView,并且不要分隔/拆分信息。



总之,你有一些破碎数据



...你可以找到这样的
This is data related. One of the lines you are splitting does not have all 4 items in it. Look at the information in your ListBox or look at where you populate your ListBox.

You can actually check for the Upper limit of lineArray before using it and just skip to the next item in the ListBox. Or consider using a different control to display your data such as a DataGridView, and don't delimit / split the information.

In short, you have some "broken" data

... which you can find like this
...
Dim lineArray() As String = myitem.Split("#")
If lineArray.GetUpperBound(0) < 3 Then
     MessageBox.Show(String.Format("This is the broken line {0}", myitem))
     Return
End If
cmd.Parameters.AddWithValue("@TITLE", lineArray(0))
---


这篇关于如何将多个项目(所有项目)从列表框插入数据库(ms.access)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆