Click here to Skip to main content
Click here to Skip to main content

Convert CSV File to data table

By , 27 Jul 2012
 

Introduction

This class allows you to convert a csv file to a data table. Useful when coding database import applications.

Background

After writing numerous database import applications I decided to create a standard class for reading CSV files and converting them into a Data Table

Using the code

The CSV Reader Class. Firstly we need to know the file location so we create a string attribute to store the file location.

Private FileName As String  

The constructor requires the file location.

 Public Sub New(ByVal FileName As String)
        Me.FileName = FileName
 End Sub

The getcolumns method returns an array of string values. These string values are the column names. these can either be  Names for the first row in the csv file, achieved by setting the columnNames  attribute true or the names are Column1, Column2,.....

  Public Function getColumns(ByVal ColumnNames As Boolean) As String()
        Try
            Dim fileReader As New StreamReader(FileName)
            Dim line As String = fileReader.ReadLine
            fileReader.Close()
            Dim Columns() As String = line.Split(",")
            If ColumnNames Then
                Return Columns
            End If
            Dim i As Integer = 1
            Dim c As Integer = 0
            Dim columnsNames(Columns.Count - 1) As String
            For Each column As String In Columns
                columnsNames(c) = "column" & i
                i += 1
                c += 1
            Next
            Return columnsNames
        Catch ex As Exception
            'log to file    
        End Try
        Return Nothing
    End Function 

The returnData method will return a data table.

    Public Function ReturnData(ByVal ColumnNames As Boolean) As DataTable
        Try
            Dim dt As New DataTable
            For Each columnName In getColumns(ColumnNames)
                dt.Columns.Add(columnName)
            Next
            Dim fileReader As New StreamReader(FileName)
            If ColumnNames Then
                fileReader.ReadLine()
            End If
            Dim line As String = fileReader.ReadLine
            While Not IsNothing(line)
                line = line.Replace(Chr(34), "")
                dt.Rows.Add(line.Split(","))
                line = fileReader.ReadLine
            End While
            fileReader.Close()
            Return dt
        Catch ex As Exception
            'log to file
        End Try
        Return Nothing
    End Function

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

EddeKreij
Software Developer Solutions Software
United Kingdom United Kingdom
I write vb applications in my spare time to generate some extra cash. Generally I would love to do this full time, so if you are looking for a developer with VB.net, C# experience and SQL. I could be your man.
 
Hobbies:
Coding, Coding, Coding
Swimming
Singing
Follow on   Twitter

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4memberAbdul Quader Mamun13-Aug-12 17:00 
GeneralMy vote of 4memberChristian Amado27-Jul-12 11:55 
BugBetter splittingmemberJoeSox27-Jul-12 9:54 
GeneralThoughtsmemberPIEBALDconsult27-Jul-12 8:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130619.1 | Last Updated 27 Jul 2012
Article Copyright 2012 by EddeKreij
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid