Friday, December 7, 2007

Import data from CSV file into data set

In this post, I am going to use the OLE DB Provider for Jet to connect to and query CSV file and then load the data into a dataset.

First of all, I will import the following namespaces:

Imports System.Data
Imports System.Data.OleDb

And then there is a function which will load data from a CSV file and return a dataset.

Private Function LoadCsvData(ByVal filename As String) As DataSet

'Create a new connection
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\;" & _
"Extended Properties='text;HDR=Yes;FMT=Delimited'")

'Query the csv file
'Example of file name – Test_CSV.csv
Dim cmd As New OleDbCommand("select * from " & filename, con)

Dim da As New OleDbDataAdapter(cmd)
Dim ds As New DataSet

'Open connection

'Fill the data set using the data adapter

'Close the connection

'Return the dataset
Return ds
End Function

Please change the location of the CSV file.



Get paid for your opinions! Click on the banner above to join Planet Pulse. Its totally free to sign up, and you can earn UNLIMITED. Find out more by visiting PLANET PULSE.
July Code Blog Copyright © 2010 Blogger Template Designed by Bie Blogger Template