Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
multiple csv files add sql server tables via VB.NET
#1
How can i multiple csv files import to database table with VB.NET ? This an important for me. Pls help
#2
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open Zip files into a listview box Worf 2 13,266 09-20-2014, 10:14 AM
Last Post: Worf
  Downloading from FTP server doesn't work brco900033 5 20,617 08-16-2013, 04:32 AM
Last Post: brco900033
  How to play random .wav files with a button click STrooper501 0 7,158 04-17-2013, 03:32 PM
Last Post: STrooper501
  How can I get the server status??? mnxford 5 21,771 03-27-2013, 10:53 AM
Last Post: mnxford
  Is this possible with SQL Server? Worf 2 11,552 02-25-2013, 03:04 AM
Last Post: Worf
  How to write string query on multiple lines kismetgerald 1 8,447 08-29-2012, 04:01 PM
Last Post: brandonio21
  Sockets Programming(Disconnecting from Server) xolara 1 8,752 04-06-2012, 01:47 PM
Last Post: brandonio21
  Problems with multiple textboxes and coding LearningVB2010 30 86,597 02-28-2011, 07:39 AM
Last Post: brandonio21

Forum Jump:


Users browsing this thread: 1 Guest(s)