本文介绍了使用OleDbConnection读取制表符分隔的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的制表符分隔文件是这样的:

My tab-delimited file is something like this:

ISO ISO3    ISO-Numeric
AD  AND 20

我一直在尝试下面的代码,但是运气不好.

I've been trying the following code with no luck.

OleDbConnection cn = new  OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= |DataDirectory|;Extended Properties='text;HDR=Yes;FMT=TabDelimited'");
OleDbCommand cmd = new OleDbCommand(@"SELECT * FROM countryInfo.txt", cn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);

cn.Open();

DataTable dt = new DataTable();
da.Fill(dt);

这是Dataset Visualizer的屏幕截图.它显然不是我追求的输出.

Here's a screenshot of the Dataset Visualizer. Its obviously not the output i'm after.

有什么建议吗?这是我的Schema.ini文件.它与文本文件位于同一目录中.

Any suggestions? Here's my Schema.ini file. Its in the same directory as the text file.

[countryInfo.txt]
Format=TabDelimited
ColNameHeader=True
CharacterSet=ANSI

我应该只使用 FileHelpers 之类的东西吗?

Should i just use something like FileHelpers instead?

@Hans Passant这是屏幕截图.

@Hans Passant Here's a screenshot.

推荐答案

好吧,一个显而易见的候选对象是该空格实际上不是制表符而是空格.尝试FMT = Delimited().使用十六进制查看器查看实际内容. 在这里.

Well, one obvious candidate is that this white space isn't actually a tab but spaces. Try FMT=Delimited( ). Use a hex viewer to see what's really there. Backgrounder is here.

并且此线程展示了为什么使用过去9年未得到支持的错误代码块(如Jet)是错误的.有了答案,将schema.ini中的第一行留空.

And this thread shows why using a buggy chunk of code like Jet that hasn't been supported for the past 9 years is such as mistake. With the answer, leave the first line in schema.ini blank.

这篇关于使用OleDbConnection读取制表符分隔的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:15