Microsoft.ACE.OLEDB.12.0 CSV 连接字符串
我知道人们时常会提出此类问题,但我找不到任何令人满意的解决方案。
如何使用 MS ACE OLEDB 12 打开 CSV 文件? 我用下面的代码尝试一下。
DbConnection connection = new OleDbConnection();
connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Documents;Extended Properties=\"Text;HDR=Yes\"";
connection.Open();
DbCommand cmd;
cmd = connection.CreateCommand();
cmd.CommandText = "SELECT * FROM [Mappe1#csv]";
DbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
Console.Write("(" + reader.GetValue(i).ToString() + ")");
Console.WriteLine();
}
cmd.Dispose();
connection.Dispose();
Console.WriteLine("Done");
Console.ReadKey();
问题是只找到一列。文本由“;”分隔。即使我用“Delimited(|)”指定分隔符,它也不起作用。
我找不到该提供商的任何文档...
I know questions this kind are asked from time to time but i can't find any satisfying solution.
How can I open a CSV-File using MS ACE OLEDB 12?
I try it with the following code.
DbConnection connection = new OleDbConnection();
connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Documents;Extended Properties=\"Text;HDR=Yes\"";
connection.Open();
DbCommand cmd;
cmd = connection.CreateCommand();
cmd.CommandText = "SELECT * FROM [Mappe1#csv]";
DbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
Console.Write("(" + reader.GetValue(i).ToString() + ")");
Console.WriteLine();
}
cmd.Dispose();
connection.Dispose();
Console.WriteLine("Done");
Console.ReadKey();
The Problem is that only one column is found. The Text is delimited by ';'. Even when i specifiy the delimiter with "Delimited(|)" f.e. it will not work.
I can't find any documentation for this provider...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这帮助我使用 ACE.OLEDB.12.0 在 C# 中解析分号分隔的 csv:
http: //sqlserverpedia.com/blog/sql-server-bloggers/use-ace-drivers-and-powershell-to-talk-to-text-files/:
创建schema.ini 与要导入的 csv 文件位于同一目录中的文本文件,其中包含以下内容:
为我工作。但太恶心了。
似乎连接字符串中的
FORMAT=Delimited(;)
已经过时了......This helped me getting a semicolon-delimited csv to parse in C# using ACE.OLEDB.12.0:
http://sqlserverpedia.com/blog/sql-server-bloggers/use-ace-drivers-and-powershell-to-talk-to-text-files/:
Create a schema.ini text file in the same directory as the csv file you want to import with the following contents:
Worked for me. But so yucky.
Seems like the
FORMAT=Delimited(;)
in the connection string has gone out of fashion...尝试:(
将“FORMAT=Delimited”插入连接字符串的扩展属性...)
Try:
(inserting "FORMAT=Delimited" into the extended properties of the connection string...)
您是否考虑过创建数据集?
Have you considered creating DataSet?