How to convert a comma seperated file (.csv) into a dataset (C#)

by Manuel 12/13/2007 9:35:00 AM
public DataSet GetCSVFile( string fileName)
{
     string pathName = System.IO. Path .GetDirectoryName(fileName);
     string file = System.IO. Path .GetFileName(fileName);
     OleDbConnection excelConnection = new OleDbConnection
     (@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ pathName + ";Extended Properties=Text;");
     OleDbCommandexcelCommand = newOleDbCommand(@"SELECT * FROM "+ file, excelConnection);
     OleDbDataAdapterexcelAdapter = newOleDbDataAdapter(excelCommand);
     excelConnection.Open();
     DataSetds = newDataSet();
     excelAdapter.Fill(ds);
     excelConnection.Close();
     return ds;
}

Currently rated 5.0 by 3 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

C# | ASP.NET

Related posts

Comments

6/19/2008 5:39:37 AM

Naresh S

I have loaded the dataset successfully,
but there is an issue.
When the csv file is physically opened by the user then this code fails with the following error.
ERROR [HY000] [Microsoft][ODBC Text Driver] The Microsoft Jet database engine cannot open the file '(unknown)'. It is already opened exclusively by another user, or you need permission to view its data.
1. The connection string must be modified with setting Readonly property = true.
I am still exploring this possibility

Naresh S in

Powered by BlogEngine.NET 1.2.0.0
Theme by Mads Kristensen

About the author

Name of author Author name
Something about me and what I do.

E-mail me Send mail

Calendar

<<  August 2010  >>
MoTuWeThFrSaSu
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345

View posts in large calendar

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Sign in