Articles → ADO.NET → Read A Pipe Separator CSV File In C#

Read A Pipe Separator CSV File In C#






Software Requirement




  1. Visual studio (any version) is installed on your machine.
  2. Any text editor (like notepad) should be there on your machine.

Technical Knowledge




  1. Basics of ado.net
  2. What is CSV format?
  3. What does encoding means?

Create A Sample Pipe Separated CSV File




Picture showing sample CSV file with pipe symbol
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


Picture showing the output of reading a pipe separater csv file in C#
Click to Enlarge


How To Specify Encoding?




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


  3. Specify CharacterSet in schema.ini
  4. [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