Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace CompareLinuxWithWindow
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15.  
  16. ConvertToDataTable(@"C:UsersmanchunlDesktopSample.txt", 10);
  17.  
  18. Console.WriteLine();
  19. Console.WriteLine("Press enter to exit.");
  20. Console.Read();
  21. }
  22.  
  23. public static DataTable ConvertToDataTable(string filePath, int numberOfColumns)
  24. {
  25. DataTable tbl = new DataTable();
  26.  
  27. for (int col = 0; col < numberOfColumns; col++)
  28. tbl.Columns.Add(new DataColumn("Column" + (col + 1).ToString()));
  29.  
  30.  
  31. string[] lines = System.IO.File.ReadAllLines(filePath);
  32.  
  33. foreach (string line in lines)
  34. {
  35. var cols = line.Split(',');
  36.  
  37. DataRow dr = tbl.NewRow();
  38. for (int cIndex = 0; cIndex < 10; cIndex++)
  39. {
  40. dr[cIndex] = cols[cIndex];
  41. }
  42.  
  43. tbl.Rows.Add(dr);
  44.  
  45. }
  46.  
  47. return tbl;
  48. }
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement