Read CSV and fill DataTable - CSharp System.Data

CSharp examples for System.Data:DataTable

Description

Read CSV and fill DataTable

Demo Code


using System.IO;/* ww  w. j  a v a2 s.c o m*/
using System.Data;

public class Main{
        public static DataTable Read(string content)
        {
            DataTable data = new DataTable();
            StringReader reader = new StringReader(content);
            string header = reader.ReadLine();
            string[] columns = header.Split(',');
            foreach(string column in columns)
            {
                data.Columns.Add(column);
            }
            string readLine;
            while ((readLine = reader.ReadLine())!= null)
            {
                string[] line = readLine.Split(',');
                DataRow row = data.NewRow();
                for (int x = 0; x < line.Length; x++)
                {
                    row[data.Columns[x]] = line[x];
                }
                data.Rows.Add(row);
            }
            reader.Close();
            return data;
        }
}

Related Tutorials