04-16-2014, 10:27 AM
Well, I'm not very familiar with databases and so. But I think the code below might, hopefully, help you.
The required imports are:
[code2=vbnet]Imports System.Data.OleDb[/code2]
You can put this code into your load button:
[code2=vbnet]Dim dlgOpenFiles As New OpenFileDialog
dlgOpenFiles.Multiselect = True
dlgOpenFiles.Filter = "CSV Files (.csv)|*.csv"
dlgOpenFiles.FilterIndex = 1
If dlgOpenFiles.ShowDialog = Windows.Forms.DialogResult.OK Then
For Each filename As String In dlgOpenFiles.FileNames
Dim cnAccess As New OleDbConnection(String.Format("provider=Microsoft.Jet.OLEDB.4.0;data source={0}\db1.mdb;", Application.StartupPath))
Dim QryInsert As String = ""
Dim QryCreate As String = ""
Dim cmd As New OleDbCommand
Dim s1 As String
Dim s2(5) As String
Dim fileReader As New IO.StreamReader(filename, False)
cmd.Connection = cnAccess
cnAccess.Open()
While (fileReader.EndOfStream = False)
s1 = fileReader.ReadLine
s2 = s1.Split(",")
QryInsert = String.Format("Insert Into Student Values ({0},'{1}',{2})", s2(0), s2(1), s2(2))
cmd.CommandText = QryInsert
cmd.ExecuteNonQuery()
End While
cnAccess.Close()
Next
MessageBox.Show("The data was imported succesfully!", "Note", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If[/code2]
Like I said I'm not very good with databases and such, so I haven't tested it fully but it should work.
I've based the code on and article I found here: http://www.planetsourcecode.com/vb/scrip...&lngWId=10
Hope it works,
Brco
The required imports are:
[code2=vbnet]Imports System.Data.OleDb[/code2]
You can put this code into your load button:
[code2=vbnet]Dim dlgOpenFiles As New OpenFileDialog
dlgOpenFiles.Multiselect = True
dlgOpenFiles.Filter = "CSV Files (.csv)|*.csv"
dlgOpenFiles.FilterIndex = 1
If dlgOpenFiles.ShowDialog = Windows.Forms.DialogResult.OK Then
For Each filename As String In dlgOpenFiles.FileNames
Dim cnAccess As New OleDbConnection(String.Format("provider=Microsoft.Jet.OLEDB.4.0;data source={0}\db1.mdb;", Application.StartupPath))
Dim QryInsert As String = ""
Dim QryCreate As String = ""
Dim cmd As New OleDbCommand
Dim s1 As String
Dim s2(5) As String
Dim fileReader As New IO.StreamReader(filename, False)
cmd.Connection = cnAccess
cnAccess.Open()
While (fileReader.EndOfStream = False)
s1 = fileReader.ReadLine
s2 = s1.Split(",")
QryInsert = String.Format("Insert Into Student Values ({0},'{1}',{2})", s2(0), s2(1), s2(2))
cmd.CommandText = QryInsert
cmd.ExecuteNonQuery()
End While
cnAccess.Close()
Next
MessageBox.Show("The data was imported succesfully!", "Note", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If[/code2]
Like I said I'm not very good with databases and such, so I haven't tested it fully but it should work.
I've based the code on and article I found here: http://www.planetsourcecode.com/vb/scrip...&lngWId=10
Hope it works,
Brco