Search code examples
c#text-filesflat-filecsv

Can't Separate Text File By Delimiter |


I am using C#.

I am trying to pull in a text file to an object. I am using an ODBC connection and it looks like this

Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=C:\Users\Owner\Desktop\IR\IR_Files\Absolute;Extensions=asc,csv,tab,txt;

I am able to make the connection but I can't get my columns separated. I'm using a schema.ini file but it isn't working. Here is my schema file.

[MyTextFile.CSV]
Format=Delimited(|)
ColNameHeader=False
Col1=fullstockn Text
col2=FULLINFO Text
MaxScanRows=0
CharacterSet=ANSI

The text file looks like this.

fullstockn|FULLINFO

"555555 "|

Contenu : Neuf Ttudes sur l Some more text here.....


Solution

  • I use the following connection string

    string connectionString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"text;HDR=YES;Format=Delimited(|)\";", Path.GetDirectoryName(path));
    

    and a Schema.ini file that typically begins

    [myFile.txt]
    Format=Delimited(|)
    TextDelimiter="none"
    

    and I'll execute a reader via

    command.CommandText = String.Format("SELECT * FROM [{0}]", Path.GetFileName(path));
    OleDbDataReader reader = command.ExecuteReader();
    

    Also, the MSDN page on the text file driver was helpful when I first investigated this. Specifically, the page on the Schema.ini file is quite useful.