Articles → ADO.NET → Read a pipe separater csv file in C#

Read a pipe separater csv file in C#




















Software requirement









Technical knowledge











Create a sample pipe separated CSV file





Click to Enlarge




Create a schema.ini file




[test.csv]
Format=Delimited(|)





Write code to read the file




string connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='text;HDR=Yes;Format=Delimited(|);'", @"C:\ ");
            string commandText = "select * from test.csv";
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                using (OleDbDataAdapter adap = new OleDbDataAdapter(commandText, conn))
                {
                    conn.Open();
                    using (DataSet ds = new DataSet())
                    {
                        adap.Fill(ds);
                    }
    }
}





Output



Click to Enlarge


How to specify encoding?






string connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='text;HDR=Yes;Format=Delimited(|);CharacterSet=65001;'", @"C:\ ");




[test.csv]
Format=Delimited(|)
CharacterSet=65001





Posted By  -  Karan Gupta
 
Posted On  -  Saturday, July 12, 2014

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250