Connect to a CSV file using ODBC [C#]
April 29th, 2007 at 11:06 pm (General)
Yesterday I am looking how to read the CSV, then i found that it’s good to use ODBC driver to read it.  Here is code what I used to read the Csv file in c#
string strConn = @”Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=C:;Extensions=csv,txt”;
try
{Â
OdbcConnection objCSV = new OdbcConnection(strConn);
objCSV.Open();
OdbcCommand oCmd = new OdbcCommand(”select * from THECSVFILE.CSV”, objCSV);
OdbcDataReader oDR = oCmd.ExecuteReader();
while (oDR.read())
{      // Do something    }
oDR.Close();
oCmd.Dispose();
objCSV.Close();
}
catch {}
Use the namespace System.Data.Odbc
In the Connection String, DBQ only points to the directory which contains the CSV files you want to work with. Use the name of the CSV file in the SQL Statement itself, as in the code above
Regards,
Elam
