We take pride in your success. We let our positivity drive us, day in and out. Talk to us at Mindfire to know us more.

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

top

VB.NET

Let us Connect!

privacy

copyright (c) Mindfire Solutions 2007-2012. Login