Articles → ADO.NET → Read A Pipe Separator CSV File In C#
Read A Pipe Separator CSV File In C#
Software Requirement
- Visual studio (any version) is installed on your machine.
- Any text editor (like notepad) should be there on your machine.
Technical Knowledge
- Basics of ado.net
- What is CSV format?
- What does encoding means?
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?
- Specify CharacterSet in connection string as shown below
string connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='text;HDR=Yes;Format=Delimited(|);CharacterSet=65001;'", @"C:\ ");
- Specify CharacterSet in schema.ini
[test.csv]
Format=Delimited(|)
CharacterSet=65001