将 csv 加载到 oleDB 中并强制所有推断的数据类型为字符串
我正在尝试使用 oledb 将 csv 文件加载到数据表中。
这没问题,但不幸的是,其中一个看似数字的字段在大约 3% 的字段中具有字符串值,因此未填充。
因为我将 csv 转换为 xml,所以我真的不关心推断数据类型,只需要字符串中的数据,因为我可以稍后在 Linq2XMl 阶段中对其进行转换。
我希望能够在连接字符串中做到这一点。
我不想只是复制表,用我想要的数据类型设置新列,然后将数据写入其中,因为这将涉及加载 csv 文件两次。
有什么想法吗?
我当前的连接字符串是
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + thefile.DirectoryName + ";Extended Properties='text;HDR=Yes;FMT=Delimited'";
Im trying to load a csv file into a datatable using oledb.
This is no problem but unfortunately one of the fields which looks numeric has a string value in about 3% of the fields and so is not being populated.
because im converting the csv into xml i really don't care about inferring datatypes and simply need the data in a string as i can cast it later in a Linq2XMl phase.
I am hoping to be able to do this in the connection string.
I don't want to just copy the table, set it up with new columns with the datatype I want and then write the data into it because that would involve loading the csv file twice.
any ideas?
my current connection string is
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + thefile.DirectoryName + ";Extended Properties='text;HDR=Yes;FMT=Delimited'";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
做了一些研究,答案是使用 schema.ini,但为您的数据集动态生成它。
http://msdn.microsoft.com/en-us/ Library/ms709353(VS.85).aspx
包含所需的信息。
构建架构:
将 csv 加载为数据表
以转换为 xml
Did some researchand the answer is use a schema.ini but generate it on the fly for your dataset.
http://msdn.microsoft.com/en-us/library/ms709353(VS.85).aspx
contains the info required.
to construct the schema:
to load the csv as datatable
to convert to xml
为了将 CSV 读入数据表,我推荐使用这个 CSV 解析器。
它真的很容易使用。以下是如何使用它用来自逗号分隔、引号限定的 CSV 的数据填充数据表:
您可以设置许多选项:分隔符、文本限定符、是否是 CSV 显示列中的第一行标题(如果为 true,DataTable 中的每个 DataColumn 都会相应地命名)等。
有许多快速、灵活的 CSV 解析器,但对于简单的要求,这个是无可匹敌的。
For reading a CSV into a DataTable I recommend this CSV parser.
It's really easy to use. Here's how you can use it to fill a DataTable with data from a comma delimited, quote qualified CSV:
There are a number of options you can set: the delimiter, the text qualifer character(s)whether the first line in the CSV show column headers (if true, each DataColumn in your DataTable will be named accordingly), etc.
There are a number of fast, flexible CSV parsers out there but for simple requirements this one can't be beat.