Click here to Skip to main content
14,773,680 members
Articles » Database » Database » General
Posted 27 Jul 2012

Tagged as


16 bookmarked

Convert CSV File to data table

Rate me:
Please Sign up or sign in to vote.
4.41/5 (9 votes)
27 Jul 2012CPOL
Convert csv file to a data table


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


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()
          Dim fileReader As New StreamReader(FileName)
          Dim line As String = fileReader.ReadLine
          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
          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
        Dim dt As New DataTable
        For Each columnName In getColumns(ColumnNames)
        Dim fileReader As New StreamReader(FileName)
        If ColumnNames Then
        End If
        Dim line As String = fileReader.ReadLine
        While Not IsNothing(line)
            line = line.Replace(Chr(34), "")
            line = fileReader.ReadLine
        End While
        Return dt
    Catch ex As Exception
        'log to file
    End Try
    Return Nothing
End Function


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


About the Author

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, C# experience and SQL. I could be your man.

Coding, Coding, Coding

Comments and Discussions

GeneralMy vote of 5 Pin
erwinmahatma15-May-15 6:34
Membererwinmahatma15-May-15 6:34 
AnswerThanks.... Pin
Nidhin.K.V19-Jun-14 1:21
MemberNidhin.K.V19-Jun-14 1:21 
GeneralMy vote of 4 Pin
Abdul Quader Mamun13-Aug-12 18:00
MemberAbdul Quader Mamun13-Aug-12 18:00 
GeneralMy vote of 4 Pin
Christian Amado27-Jul-12 12:55
professionalChristian Amado27-Jul-12 12:55 
BugBetter splitting Pin
JoeSox27-Jul-12 10:54
MemberJoeSox27-Jul-12 10:54 
GeneralThoughts Pin
PIEBALDconsult27-Jul-12 9:00
professionalPIEBALDconsult27-Jul-12 9:00 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.