BP Forums
multiple csv files add sql server tables via VB.NET - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: VB.NET (Visual Basic 2010/2008) (https://bpforums.info/forumdisplay.php?fid=8)
+----- Forum: Programming Help (https://bpforums.info/forumdisplay.php?fid=9)
+----- Thread: multiple csv files add sql server tables via VB.NET (/showthread.php?tid=830)



multiple csv files add sql server tables via VB.NET - secilsengul - 04-14-2014

How can i multiple csv files import to database table with VB.NET ? This an important for me. Pls help


Re: multiple csv files add sql server tables via VB.NET - brco900033 - 04-16-2014

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/scripts/ShowCode.asp?txtCodeId=9018&lngWId=10


Hope it works,
Brco