Creating a DataTable from CSV File

前端 未结 5 2087
野趣味
野趣味 2020-12-10 06:59

I\'m working on a project and I need to read a CSV file and then fill a DataSet with its data. I\'ve been searching and I have found some interesting things in OleDB.

5条回答
  •  星月不相逢
    2020-12-10 07:23

    if nothing special i use this kind of code

    TextReader tr1 = new StreamReader(@"c:\pathtofile\filename",true);
    
    var Data = tr1.ReadToEnd().Split('\n')
    .Where(l=>l.Length>0)  //nonempty strings
    .Skip(1)               // skip header 
    .Select(s=>s.Trim())   // delete whitespace
    .Select(l=>l.Split(',')) // get arrays of values
    .Select(l=>new {Field1=l[0],Field2=l[1],Field3=l[2]});
    

提交回复
热议问题