Reading a CSV file in .NET?

Solution 1:

A choice, without using third-party components, is to use the class Microsoft.VisualBasic.FileIO.TextFieldParser (http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.aspx) . It provides all the functions for parsing CSV. It is sufficient to import the Microsoft.VisualBasic assembly.

var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(file);
parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
parser.SetDelimiters(new string[] { ";" });

while (!parser.EndOfData)
{
    string[] row = parser.ReadFields();
    /* do something */
}

Solution 2:

You can use the Microsoft.VisualBasic.FileIO.TextFieldParser class in C#:

using System;
using System.Data;
using Microsoft.VisualBasic.FileIO;

static void Main()
{
    string csv_file_path = @"C:\Users\Administrator\Desktop\test.csv";

    DataTable csvData = GetDataTableFromCSVFile(csv_file_path);

    Console.WriteLine("Rows count:" + csvData.Rows.Count);    
    Console.ReadLine();
}

private static DataTable GetDataTableFromCSVFile(string csv_file_path)
{
    DataTable csvData = new DataTable();

    try
    {
        using(TextFieldParser csvReader = new TextFieldParser(csv_file_path))
        {
            csvReader.SetDelimiters(new string[] { "," });
            csvReader.HasFieldsEnclosedInQuotes = true;
            string[] colFields = csvReader.ReadFields();

            foreach (string column in colFields)
            {
                DataColumn datecolumn = new DataColumn(column);
                datecolumn.AllowDBNull = true;
                csvData.Columns.Add(datecolumn);
            }

            while (!csvReader.EndOfData)
            {
                string[] fieldData = csvReader.ReadFields();
                //Making empty value as null
                for (int i = 0; i < fieldData.Length; i++)
                {
                    if (fieldData[i] == "")
                    {
                        fieldData[i] = null;
                    }
                }

                csvData.Rows.Add(fieldData);
             }
         }
     }
     catch (Exception ex)
     {
     }

     return csvData;
}