Click here to Skip to main content
15,885,366 members
Articles / Web Development / ASP.NET

A Fast CSV Reader

Rate me:
Please Sign up or sign in to vote.
4.93/5 (545 votes)
13 Jan 2016MIT8 min read 8.3M   164.8K   1.5K  
A reader that provides fast, non-cached, forward-only access to CSV data.
#region Using directives

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

#endregion

namespace CsvReaderDemo
{
	public sealed class OleDbBenchmark
	{
		private OleDbBenchmark()
		{
		}

		public static object Run(object[] args)
		{
			if (args.Length == 1)
				Run((string) args[0]);
			else
				Run((string) args[0], (int) args[1]);

			return null;
		}

		public static void Run(string path)
		{
			Run(path, -1);
		}

		public static void Run(string path, int field)
		{
			string directory = Path.GetDirectoryName(path);
			string file = Path.GetFileName(path);

			using (OleDbConnection cnn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + directory + @";Extended Properties=""Text;HDR=No;FMT=Delimited"""))
			{
				using (OleDbCommand cmd = new OleDbCommand(@"SELECT * FROM " + file, cnn))
				{
					cnn.Open();

					using (OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
					{
						string s;

						if (field == -1)
						{
							while (dr.Read())
							{
								for (int i = 0; i < dr.FieldCount; i++)
									s = dr.GetValue(i) as string;
							}
						}
						else
						{
							while (dr.Read())
								s = dr.GetValue(field) as string;
						}
					}
				}
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Architect
Canada Canada
Sébastien Lorion is software architect as day job.

He is also a musician, actually singing outside the shower Smile | :)

He needs constant mental and emotional stimulation, so all of this might change someday ...

Comments and Discussions