Sunday, March 14, 2010

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

A small program to convert .csv file into a dataset which returns a dataset.

public DataSet CSVFileParser( 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;");
     OleDbCommand excelCommand = newOleDbCommand(@"SELECT * FROM "+ file, excelConnection);
     OleDbDataAdapter excelAdapter = newOleDbDataAdapter(excelCommand);
     excelConnection.Open();
     DataSet ds = new DataSet();
     excelAdapter.Fill(ds);
     excelConnection.Close();
     return ds;
}

Hope this is helpful. Happy coding!

No comments:

Post a Comment