Question

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

Was it helpful?

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.

OTHER TIPS

Is there a reason you need to use an ODBC connection for this? I would think it'd be easier to just open the text file directly and parse it yourself.

I don't know if this matters but...

You might be missing the ending "\" in your dbq attribute...

EDIT: Actually...in the text you posted, you have 3 columns, not 2...(2 pipes instead of 1)

I always write the code myself for this kind of op. Here is an example of an abstract class I wrote for this purpose not so long ago. You could modify it or subclass it if you like

public abstract class ReadTextFile : ILoadable
{
    public string Path { get; set; }
    public UploadFileType FileType { get; set; }
    protected internal List<List<string>> Table { get; set; }
    public Guid BatchID { get; set; }

    /// <summary>
    /// Method that loads the raw text into a collection of strings
    /// </summary>
    /// <returns></returns>
    public bool Load()
    {
        Table = new List<List<string>>();
        var splitter = Convert.ToChar("\t");
        try
        {
            using (TextReader tr = new StreamReader(Path))
            {
                // Discard the first line
                String line = tr.ReadLine();

                // Read and display lines from the file until the end of the file is reached.
                while ((line = tr.ReadLine()) != null)
                {
                    Table.Add(line.Split(splitter).ToList<string>());
                }
                tr.Close();
                tr.Dispose();
            }
            return true;

        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            return false;
        }
    }
    public string Left(string param, int length)
    {
        //we start at 0 since we want to get the characters starting from the
        //left and with the specified lenght and assign it to a variable
        string result = param.Substring(0, length);
        //return the result of the operation
        return result;
    }
    public string Right(string param, int length)
    {
        //start at the index based on the lenght of the sting minus
        //the specified lenght and assign it a variable
        string result = param.Substring(param.Length - length, length);
        //return the result of the operation
        return result;
    }
}

Try using this connection string

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Owner\Desktop\IR\IR_Files\Absolute\MyTextFile.CSV;Extended Properties='text'

and:

  • Beware of the number of columns
  • Place the schema.ini in the same folder of the executable.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top