CSV to DataTabe C# all strings

this sln is for when u know / take all the columns as strings, but its very fast and simple.
thx to Alan here for the base:
http://www.c-sharpcorner.com/Forums/Thread/47618/C-Sharp-importing-csv-files-into-datatables.aspx
public static DataTable TransferCSVToTable(string filePath)
{
   DataTable dt = new DataTable();
   string[] csvRows = System.IO.File.ReadAllLines(filePath);
   string[] fields = null;
   bool first = true;

   foreach (string csvRow in csvRows)
   {
      fields = csvRow.Split(
',');

      if (first)
      {
     
   foreach (string header in fields)
         dt.Columns.Add(new DataColumn(header));
         first = false;
      }
      else
      {
         DataRow row = dt.NewRow();
         row.ItemArray = fields;
         dt.Rows.Add(row);
      }
   }
   return dt;
}

Comments

Popular posts from this blog

OverTheWire[.com] Natas Walkthrough - JUST HINT, NO SPOILERS

SOLVED The item could not be indexed successfully because the item failed in the indexing subsystem

Asp.Net Ending Response options, Response.End() vs CompleteRequest()