Monday, August 6, 2012

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:
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;
         DataRow row = dt.NewRow();
         row.ItemArray = fields;
   return dt;

No comments:

Post a Comment