带有句点字符C#数据表的CSV标头 [英] CSV header with period character C# datatable

查看:68
本文介绍了带有句点字符C#数据表的CSV标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试使用以下代码将CSV文件解析为数据表:

Hi all,

I am trying to parse CSV file to a datatable using this code:

//create the "database" connection string
      string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
        + "Data Source=\"" + dir + "\\\";"
        + "Extended Properties=\"text;HDR=Yes;FMT=Delimited\"";
      //create the database query
      string query = "SELECT * FROM " + file;
      //create a DataTable to hold the query results
      DataTable dTable = new DataTable();
      //create an OleDbDataAdapter to execute the query
      OleDbDataAdapter dAdapter =
         new OleDbDataAdapter(query, connString);
      try
      {
        //fill the DataTable
        dAdapter.Fill(dTable);
      }
      catch (InvalidOperationException /*e*/)
      { }



CSV文件的第一行定义为标题.标头中有一列名为时间点0.5(s)".将其解析到数据表时,该列显示为时间点0#5(s)".这 "."字符变为#"字符.如果该行不是标题,则不会发生这种情况.我该如何解决这个问题?

问候,



First line of the CSV file is defined as a header. There is a column named "Time Point 0.5(s)" in the header. When parsing it to the datatable, the column appeared as "Time Point 0#5(s)". The "." character becomes "#" character. This does not happen if the row is not header. How can I solve this problem?

Regards,

推荐答案

看看 [ ^ ]博客.这是将CSV转换为数据表的另一种方法.它可能会解决您的问题.除此之外,您还可以尝试 [ [ ^ ]链接以进一步了解.
Have a look at this [^]blog. This is other way converting CSV into datatable. It might solve your problem. In addition to this you can try the this[^] CSV parser.



This because, if you want try to bind your DataTable with such column it might throw exception due to (.) period character. For example if you try to bind the converted CSV records to DataBinder with Eval Method, it will throw exception. So to minimize such exception DataTable will convert the Column Name to other special character. See this[^] link for further.




感谢你的回复.我确实读过那些文章,但是,我很想知道这个问题背后的原因,为什么是."字符变成#"字符,就好像它在标题行中一样.在数据行中就可以了.

谢谢
Hi,

Thank you for your reply. I did read those articles before, however, I would love to know the reason behind this problem, why "." character becomes "#" character as if it is in header row. While in the data row, it is fine.

Thanks,


这篇关于带有句点字符C#数据表的CSV标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆