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)
            Dim cmd As New OleDbCommand("SELECT * FROM [" & strFileName & "]", conn)
            Dim da As New OleDbDataAdapter()
            da.SelectCommand = cmd
            Dim ds As New DataSet()


            Return ds.Tables(0)
            Return Nothing
        End Try
 End Function

Related Tags:

VB.NET, CSV , Table

Author: Shibani Shubhadarshini


Let us Connect!

Awards and Achievements

Red Herringcolor DeloitteFast50 DB ZInnov1   Nascome  DB Stpi

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