I am working on a project and I need to read the CSV file and then populate the DataSet with my data. I searched and I found interesting things in OleDB.
I have a CSVReader class:
class CSVReader { public DataTable GetDataTable(string filePath) { OleDbConnection conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + Path.GetDirectoryName(filePath) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\""); conn.Open(); string strQuery = "SELECT * FROM [" + Path.GetFileName(filePath) + "]"; OleDbDataAdapter adapter = new OleDbDataAdapter(strQuery, conn); DataSet ds = new System.Data.DataSet("CSV File"); adapter.Fill(ds); return ds.Tables[0]; } }
And I call it from here:
CSVReader datareader = new CSVReader(); DataTable dt = datareader.GetDataTable(filepath);
The problem is that it parses the first row (header row), such as JUST ONE for the column, I mean: This is the header of the CSV file:
Name, Product Name, Server, Vendor, Start Time, End Time, Host Name, User Name, Project Name, Usage time (hours)
And after it, all data is separated by commas.
When I read the file, fill in the dataset and type dt.Columns.Count, it shows that it has only 1 column.
Any help?
Thanks in advance.
c # visual-studio
Pablo reyes
source share