Not logged in - Login

Import a CSV file

Snippet of code to import a CSV file using C#.

Be sure to add Microsoft.VisualBasic to your references.

// create an instance of our text field parser object.
using (Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(@"c:\temp\testimport.csv"))
       // set some properties on the parser object.
       parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;

       // keep reading the file until we get to the end.
       while (!parser.EndOfData)
              // process each row. Read the fields in the current line.
              string[] fields = parser.ReadFields();
              // Process fields here

       // accept all our changes to the table.

See also Parse CSV

Inspired by Reading CSV files using C#

Links for more information: