gpt4 book ai didi

c# - 使用 OleDbCommand/OleDbDataAdapter 读取 CSV 文件

转载 作者:行者123 更新时间:2023-11-30 22:42:56 27 4
gpt4 key购买 nike

我不知道为什么,但是当我使用 OleDbDataAdapter 或 OleDbCommand 读取 CSV 文件时,在这两种情况下,结果数据的结构都很好(它识别文件头中的列),但行数据都是空的字符串。

我以前做过很多次 CSV 处理并成功,所以我发现这个文件的唯一区别是文件中的所有字段都用引号引起来。
我试过使用 schema.ini 文件,但无济于事。这感觉像是 Microsoft Jet 4.0 组件的问题,但我无法弄清楚问题出在哪里。

这是文件的摘录:

"UNIQUEID","OWNERID","PHONE1","PHONE2","EMERGENCYCONTACT","ADDRESS1","ADDRESS2","ADDRESS3","ADDRESSCITY","ADDRESSSTATE","ADDRESSZIP","UNIONCODE","CUSTOM1","CUSTOM2","CUSTOM3","CUSTOM4","CUSTOM5","CUSTOM6"
"5","33","1235551212"," ","","1914 SANDFLAT ROAD","","","THOMASVILLE","AL","367849215","","contract","7.75","1","N","","A"
"6","34","1235551212"," ","","1407 OLD HWY. 5 SOUTH","","","THOMASVILLE","AL","36784","","contract","7.75","1","N","","B"
"7","35","1235551212"," ","","P.O. BOX 204","","","THOMASVILLE","AL","36784","","substitute","7.75","0","Y","","M"
"8","36","1235551212"," ","","383 UNCLE BEN RD","","","THOMASVILLE","AL","36784","","substitute","0.00","0","","",""

这是我当前的代码:

OleDbConnection conn = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\payroll;" +
"Extended Properties=\"text;HDR=Yes;FMT=CSVDelimited\"");
OleDbDataAdapter adapter = new OleDbDataAdapter("select * from file.txt", conn);
DataTable dt = new DataTable();
adapter.Fill(dt);

这里是 schema.ini 文件的内容。没有它我无法读取文件:

[file.txt]
Format=CSVDelimited
ColNameHeader=True
Col1="UNIQUEID" Text
Col2="OWNERID" Text
Col3="PHONE1" Text
Col4="PHONE2" Text
Col5="EMERGENCYCONTACT" Text
Col6="ADDRESS1" Text
Col7="ADDRESS2" Text
Col8="ADDRESS3" Text
Col9="ADDRESSCITY" Text
Col10="ADDRESSSTATE" Text
Col11="ADDRESSZIP" Text
Col12="UNIONCODE" Text
Col13="CUSTOM1" Text
Col14="CUSTOM2" Text
Col15="CUSTOM3" Text
Col16="CUSTOM4" Text
Col17="CUSTOM5" Text
Col18="CUSTOM6" Text
CharacterSet=ANSI

最佳答案

我发现了问题所在。 schema.ini 文件中的 CharacterSet 设置为 ANSI。文件编码为 Unicode。将 CharacterSet 设置为 Unicode 解决了这个问题。

关于c# - 使用 OleDbCommand/OleDbDataAdapter 读取 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4165342/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com