Using OLE to connect to a text file

This code demonstrates how to use the OLE database driver in order to connect to a text file.

using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Text;

namespace OLEDBCSVDataSource
class CSVDataExample
private const string OLE_CONN_STRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};" +
"Extended Properties='text;HDR=Yes;FMT=Delimited'";

static void Main( string[] args )

string sFileName = "CSVData.txt";
string sExecPath = Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location );
string sDataSource = Path.Combine( sExecPath, sFileName );
string sConn = String.Format( OLE_CONN_STRING, sExecPath );

using ( OleDbConnection conn = new OleDbConnection( sConn ) )
OleDbCommand cmdSelect = new OleDbCommand( "SELECT * FROM " + sFileName, conn );
OleDbDataReader reader = cmdSelect.ExecuteReader();
while( reader.Read() )
int iFieldCount = reader.FieldCount;
StringBuilder sb = new StringBuilder();
for( int i = 0; i < iFieldCount; i++ )
sb.Append( reader.GetValue( i ).ToString() );
sb.Append( "\t" );
Console.WriteLine( sb.ToString() );

You might also like...


Lee Gunn - .NET C# Scotland Lee Gunn is a freelance Microsoft Certified Developer based in Glasgow, Scotland. Specialising in quality driven Internet solutions, largely built around Microsoft’s .NET platform. Skills used o...


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are 10 types of people in the world, those who can read binary, and those who can't.”