且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何正确读取文本文件中的列

更新时间:2023-02-20 11:00:21

我通常采用一个平凡的解决方案,文件,我读取所有行,在循环中我拆分行字符串,创建和填充一个新的数据行,最后我添加数据行到数据表:

I usually adopt a trivial solution, i.e. I access the file, I read all lines, in a loop I split the line string, create and populate a new datarow, and finally I add the data row to the datatable:

string[] records = File.ReadAllLines(path);
foreach(string record in records)
{
  DataRow r = myDataTable.NewRow();
  string[] fields = record.Split('\t');
  /* Parse each field into the corresponding r column
   * ....
   */
  myDataTable.rows.Add(r);
}

我还发现了如何使用OleDb连接访问CSV文件的解决方案,模式信息文件。我从未使用过这种方法。

I have also found solutions regarding how to access CSV files with OleDb connections, and schema information files. I have never used this approach.

参考文献:

  • File.ReadAllLInes().
  • String.Split().
  • *** related question with OleDb connection.
  • MSDN Schema Information File.