What's the best way to read CSV data?

I am using C # and am trying to read CSV using this connection string;

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\rajesh.yadava\Desktop\orcad;Extended Properties="Text;HDR=YES;IMEX=1;FMT=Delimited"

      

This works for tab-delimited data.

I need a connection string that needs to be tab-separated, as well as a comma (,) and pipe (|).

How to create a generic connection string for CSV.

Thanks Rajesh

+1


source to share


7 replies


Is there a filehelpers option ?



+2


source


I know this doesn't answer your questions, but here's a word of warning.

I had to create my own reader as you won't get the correct drivers if you ever run a 64 bit system.



If your software will ever run on a 64-bit system, make sure you test it first and that the oledb or odbc drivers will be present.

+1


source


If you need fast sequential access to a CSV file, Fast CSV Reader might be an option. I used it on a project a while ago with great success. It is supposed to be optimized well enough and also provide a cached version if you need it. In addition, it has been updated several times since its first release in 2005 (last updated 2008-10-09) and supports basic data binding through implementation System.Data.IDataReader

.

+1


source


0


source


0


source


Without the caret of a custom solution, I'm not sure if there is an easy way to support more than one delimiter. This page assumes that through schema.ini you can choose between:

  • TabDelimited
  • CSVDelimited
  • one specific character (other than the double quote)
  • fixed width
0


source


    class CSVFile extends SplFileObject
{

private $keys;

    public function __construct($file)
    {
        parent::__construct($file);
        $this->setFlags(SplFileObject::READ_CSV);
    }

    public function rewind()
    {
        parent::rewind();
        $this->keys = parent::current();
        parent::next();
    }

    public function current()
    {
        return array_combine($this->keys, parent::current());
    }

    public function getKeys()
    {
        return $this->keys;
    }
}

      

then use with:

$csv = new CSVFile('exmaple.csv');

      

and you can iterate over the lines using:

foreach ($csv as $line)
{

      

0


source







All Articles