How do I correctly handle CR when reading text files with OleDB

假如想象 提交于 2020-01-06 05:34:11

问题


I have text files that are Tab delimited. I created a Schema.ini like so:

[MY_FILE.TAB]
Format=TabDelimited
ColNameHeader=False
Col1=id Short
Col2=data Text

This is the code I use to read it (C#):

using (var connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\FolderToData\;Extended Properties='text;FMT=delimited'"))
{
  using (var command = new OleDbCommand("SELECT * FROM MY_FILE.TAB", connection))
  {
    var table = new DataTable();
    using (var adapter = new OleDbDataAdapter(command)
    {
      adapter.Fill(table);
    }
  }
}

Everything works fine, except for one thing. The data in the text file contains Carriage Returns [CR]. Records itself are separated by Carriage Return Line Feeds [CR][LF]. Unfortunately, OleDB / MicrosoftJet (or whatever parses these files) treats both ([CR], [CRLF]) the same.

Example of MY_FILE.TAB (there should be a Tab between numbers and text):

1   One[CR][LF]
2   Two[CR][LF]
3   Th[CR]
ree[CR][LF]
4   Four[CR][LF]

Gives me 5 (malformed) Rows in the DataTable instead of 4.

What I need is:

1   "One"
2   "Two"
3   "Th\nree"
4   "Four2

But I get:

1    "One"
2    "Two"
3    "Th"
null null
4    "Four"

"ree" can't be converted to Int32 so first colum in fourth row is null.

How can I configure OleDB to treat [CR] different than [CR][LF]? Or any other Ideas?


回答1:


I don't believe you can reconfigure OLEDB to do this directly.

An alternative approach would be to use a TextReader and TextWriter to process the file into a temporary file, scanning for and replacing CR alone into some special escape sequence. Then use OLEDB to read this replacement temporary file; finally, replace the special escape sequence back to a CR.




回答2:


Wouldn't it be easy to read the file contents in a string, split it by Environment.NewLine or \r\n, which will get you an array for each line, which you can further split by tab?



来源:https://stackoverflow.com/questions/1809272/how-do-i-correctly-handle-cr-when-reading-text-files-with-oledb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!