简体   繁体   English

使用C#使用OleDb解析CSV

[英]Parsing CSV using OleDb using C#

I know this topic is done to death but I am at wits end. 我知道这个话题已经完成了死亡,但我最终处于斗智斗勇。

I need to parse a csv. 我需要解析一个csv。 It's a pretty average CSV and the parsing logic has been written using OleDB by another developer who swore that it work before he went on vacation :) 这是一个非常普通的CSV和解析逻辑是由另一个开发人员使用OleDB编写的,他发誓它在去度假之前工作:)

CSV sample:
Dispatch Date,Master Tape,Master Time Code,Material ID,Channel,Title,Version,Duration,Language,Producer,Edit Date,Packaging,1 st TX,Last TX,Usage,S&P Rating,Comments,Replace,Event TX Date,Alternate Title
,a,b,c,d,e,f,g,h,,i,,j,k,,l,m,,n,

The problem I have is that I get various errors depending on the connection string I try. 我遇到的问题是我遇到了各种错误,具体取决于我尝试的连接字符串。

when I try the connection string: 当我尝试连接字符串时:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source="D:\TEST.csv\";Extended Properties="text;HDR=No;FMT=Delimited"

I get the error: 我收到错误:

'D:\TEST.csv' is not a valid path.  Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.

When I try the connection string: 当我尝试连接字符串时:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\TEST.csv;Extended Properties=Excel 12.0;

or the connection string 或连接字符串

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\TEST.csv;Extended Properties=Excel 8.0;

I get the error: 我收到错误:

External table is not in the expected format.

I am considering throwing away all the code and starting from scratch. 我正在考虑丢弃所有代码并从头开始。 Is there something obvious I am doing wrong? 有什么明显的东西我做错了吗?

You should indicate only the directory name in your connection string. 您应该仅在连接字符串中指明目录名称。 The file name will be used to query: 文件名将用于查询:

var filename = @"c:\work\test.csv";
var connString = string.Format(
    @"Provider=Microsoft.Jet.OleDb.4.0; Data Source={0};Extended Properties=""Text;HDR=YES;FMT=Delimited""", 
    Path.GetDirectoryName(filename)
);
using (var conn = new OleDbConnection(connString))
{
    conn.Open();
    var query = "SELECT * FROM [" + Path.GetFileName(filename) + "]";
    using (var adapter = new OleDbDataAdapter(query, conn))
    {
        var ds = new DataSet("CSV File");
        adapter.Fill(ds);
    }
}

And instead of OleDB you could use a decent CSV parser (or another one ). 而不是OleDB你可以使用一个体面的CSV解析器 (或另一个 )。

Alternate solution is to use TextFieldParser class (part of .Net framework itself.) https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.fileio.textfieldparser 替代解决方案是使用TextFieldParser类(.Net框架本身的一部分。) https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.fileio.textfieldparser

This way you do not have to rely on other developer who has gone for holidays. 这样您就不必依赖已经去度假的其他开发人员。 I have used it so many times and have not hit any snag. 我已经多次使用它并没有遇到任何障碍。

I have posted this from work (hence I cannot post an example snippet. I will do so when I go home this evening). 我已经从工作中发布了这个(因此我无法发布示例代码段。我今晚回家时会这样做)。

看来你的第一行包含列名,所以你需要包含HDR = YES属性,如下所示:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\TEST.csv;Extended Properties="Excel 12.0;HDR=YES";

尝试连接字符串:

"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\TEST.csv;Extended Properties=\"Excel 8.0;IMEX=1\""
 var  s=@"D:\TEST.csv";
 string dir = Path.GetDirectoryName(s);
 string sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;"
                       + "Data Source=\"" + dir + "\\\";"
                       + "Extended Properties=\"text;HDR=YES;FMT=Delimited\"";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM