1.使用OleDB操作.csv文件,比较费时
1 public static DataTable GetDataTableFromCsv(string path,bool isFirstRowHeader) 2 { 3 string header = isFirstRowHeader ? "Yes" : "No"; 4 5 string pathOnly = Path.GetDirectoryName(path);//得到文件夹路径,相当于得到要操作的“数据库” 6 string fileName = Path.GetFileName(path);//得到文件名,相当于得到要操作的数据库中的数据表 7 8 string sql = @"select * from ["+fileName+"]";//sql语句 9 //操作数据库的流程 10 using (OleDbConnection con=new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly +";Extended Properties=\"Text;HDR=" + header + "\"")) 11 { 12 using (OleDbCommand cmd=new OleDbCommand(sql,con)) 13 { 14 using (OleDbDataAdapter adapter=new OleDbDataAdapter(cmd)) 15 { 16 DataTable dt=new DataTable(); 17 dt.Locale=CultureInfo.CurrentCulture; 18 adapter.Fill(dt); 19 return dt; 20 } 21 } 22 } 23 }