This is my blog.
function
Published on February 7, 2005 By Foxjazz In Software Development

Well I found a cool piece of code to import CSV files via oleDB in .net.

The code I found was ok, it was in c#, however I translated to vb.net (cause that is what I am currently using).

Here is the code:

'You must provide the dataTable you want to fill, and the Fully qualified filename (path & filename).

Public Shared Function FillDataTableFromText(ByRef dt As DataTable, ByVal file As String)

dt = New DataTable

Dim conn As OleDbConnection

Dim sql As String

Dim FileConnection As String

Dim oda As New OleDbDataAdapter

Dim msg As String

FileConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="

FileConnection += Path.GetDirectoryName(file) & ";Extended Properties=""Text;HDR=YES;"""

Try

Try

conn = New OleDbConnection

conn.ConnectionString = FileConnection

conn.Open()

Catch ex As Exception

msg = ex.Message

End Try

sql = String.Format("SELECT * FROM {0}", Path.GetFileName(file))

oda = New OleDbDataAdapter(sql, conn)

oda.SelectCommand.ExecuteNonQuery()

oda.Fill(dt)

Catch ex As Exception

End Try

oda.Dispose()

conn.Dispose()

End Function


Comments
on Feb 09, 2005
There are really easy ways to do this using Enterprise Manager as well. Not sure if you just needed a programmatic way to do this or if you just needed to get the data from a CSV into a DB.
on Feb 09, 2005
A dataTable is an inMemory Table that can belong to an in memory database. Not sure why you would want to import a flatfile directly to a database anyway. A flat file can represent many tables and changes in a database that need to go through a process for updating data in a database. Also flat files are normally not normalized.

This code is highly useful when creating a datainterface. Take an excel spreadsheet or a flatfile created from excell or access, and run them through an interface program that manages every record and updates the database as necessary.

Excel Only supports 64K records, and I may have 200K records in my file, instead of parsing every line it is much easier to let OleDb do all that work.
on Feb 09, 2005
Hmmm, interesting.. I've never actually had the need to work with entire tables in memory but if I do, I now know where to turn.

As far as importing to a database from a CSV file I've had to do it lots of times. Getting information from an accounting department for one. The users in the accounting department are able to export from their financial app to excel, and then we import the necessary data into our systems to make it available to the appropriate individuals. It's just something that they are comfortable with and as our customers we do what they want, whether it's the best way to do it or not. Such is life I suppose.
on Mar 30, 2005
Hi,

I tried out your example after converting it to C#.Net, however i get an error when opening the connection "Could not find Installable ISAM"?? i can't seem to find any info on what is causing this?? do you still have the original version that you converted to VB.net? or can you suggest a reason why i am getting that error