读取 CSV 数据的最佳方式是什么?
我正在使用 C# 并尝试使用此连接字符串读取 CSV
;
Provider=Microsoft.Jet.OLEDB.4.0;数据源=C:\Documents and Settings\rajesh.yadava\Desktop\orcad;扩展属性=“Text;HDR=YES;IMEX=1;FMT=”分隔”
这适用于制表符分隔的数据。
我想要一个连接字符串,它应该用于制表符分隔以及逗号(,)和管道(|)。
如何为 CSV
创建通用连接字符串。
谢谢 拉杰什
I am using C# and trying to read a CSV
by using this connection string;
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\rajesh.yadava\Desktop\orcad;Extended Properties="Text;HDR=YES;IMEX=1;FMT=Delimited"
This works for tab delimited data.
I want a connection string which should for tab delimited as well as comma(,) and pipe(|).
How can I make a generic connection string for CSV
.
Thanks
Rajesh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
filehelpers 库是一个选项吗?
Is the filehelpers library an option?
我知道这并不能回答你的问题,但这里有一个警告。
我必须创建自己的阅读器,因为如果您在 64 位系统上运行,则无法获得正确的驱动程序。
如果您的软件将在 64 位系统上运行,请确保首先对其进行测试,并且 oledb 或 odbc 驱动程序将存在。
I know this doesn't answer your questions, but here's a word of warning.
I've had to create my own reader as you don't get the correct drivers if you ever run on a 64 bit system.
If your software will ever run on a 64 bit system, make sure you test it first and that the oledb or odbc drivers will be present.
如果您需要快速顺序访问 CSV 文件,快速 CSV 阅读器 可能是一个选项。 我前段时间在一个项目中使用过它,取得了巨大的成功。 它应该经过很好的优化,并且如果需要的话还提供缓存版本。 此外,自 2005 年首次发布(最后一次更新于 2008 年 10 月 09 日)以来,它已更新多次,并且通过实现 System.Data.IDataReader 支持基本数据绑定。
In case that you need a fast sequential access to the CSV file, the Fast CSV Reader could be an option. I have used it on a project some time ago with great success. It is supposed to be optimized quite well and also provides a cached version, if you need it. Additionally, it was updated several times since it was first released back in 2005 (last update in 2008-10-09) and it supports basic databinding by implementing
System.Data.IDataReader
.以下是讨论此问题的一些网络链接:
Here's a few links from the net discussing this issue:
使用 TextFieldParser 类是一种选择吗?
http://msdn.microsoft.com/en-我们/library/microsoft.visualbasic.fileio.textfieldparser.aspx
Is using the TextFieldParser class an option?
http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.aspx
如果不推出自定义解决方案,我不确定是否有一种简单的方法来支持多个分隔符。 此页面建议通过 schema.ini 您可以选择:
Without rolling a custom solution, I'm not sure there's a straightforward way to support more than one delimiter. This page suggests that through schema.ini you can choose between:
然后使用 with:
并且您可以使用以下方式迭代行:
then use with:
and you can iterate through lines using: