Software Technology Tips

The following code lines can be used to read a file with .CSV extension and extract all records in the CSV file to a Datatable.

We need to pass the complete Folder path and File name of the CVS file as the parameters to the function which will return a datatable with records from CVS file.

Create a connection string for OleDbConnection where datasource is the CSV filepath. Open connection and use query to extract records from CSV file.

VB.Net Code :
 
 Public Function GetCsvData(ByVal strFolderPath As String, ByVal strFileName As String) As DataTable
 
        Dim strConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFolderPath & ";Extended Properties=Text;"
        Dim conn As New OleDbConnection(strConnString)
 
        Try
            conn.Open()
            Dim cmd As New OleDbCommand("SELECT * FROM [" & strFileName & "]", conn)
            Dim da As New OleDbDataAdapter()
 
            da.SelectCommand = cmd
 
            Dim ds As New DataSet()

            da.Fill(ds)
            da.Dispose()

            Return ds.Tables(0)
        Catch
            Return Nothing
        Finally
            conn.Close()
        End Try
 
 End Function


Related Tags:

VB.NET, CSV , Table

Author: Shibani Shubhadarshini

VB.NET

Let us Connect!

iso 9001 QA25 Red Herring STPI D&B Fastest Growing SME 2013 Award zinnov Nasscom

This site uses cookies. We respect your privacy.copyright (c) Mindfire Solutions 2007-2015. Login