Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
748 views
in Technique[技术] by (71.8m points)

c# - Microsoft.ACE.OLEDB.12.0 CSV ConnectionString

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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

[fileIwantToImport.csv]
Format=Delimited(;)
ColNameHeader=True

Worked for me. But so yucky.

Seems like the FORMAT=Delimited(;) in the connection string has gone out of fashion...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...