I am trying to read a csv file using following code. The file has columns headers also.
Following is the output after loading the file in dataset.
public DataSet LoadCVS(string filePath)
{
DataSet ds = new DataSet();
string fileName = System.IO.Path.GetFileName(filePath);
try
{
string path = @System.IO.Path.GetDirectoryName(filePath);
using (OleDbConnection conn =
new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + @";Extended Properties=""Text;HDR=Yes;FMT=Delimited"""))
{
using (OleDbCommand cmd =
new OleDbCommand("SELECT * FROM [" + fileName + "]", conn))
{
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds, "csv");
}
}
}
catch (Exception ex) //Error
{
MessageBox.Show(ex.Message);
}
return ds;
}
Sample data of csv file:
Org,Item Number,Item Description,Lot Number,Lot Expiration Date,Marketing Division,Product Type
F01,Jan-00,LFIT MORSE TAPER HEAD,MHD7D8,3-May-14,5,5
F01,Jan-05,LFIT MORSE TAPER HEAD,MHATY9,1-Mar-14,5,5
F01,Jan-00,LFIT MORSE TAPER HEAD,MHDEN1,8-Mar-14,5,5
F01,Jan-05,LFIT MORSE TAPER HEAD,MHNY4L,18-Nov-14,5,5
F01,Jan-10,LFIT MORSE TAPER HEAD,MHHLYR,31-May-14,5,5
F01,Jan-00,LFIT MORSE TAPER HEAD,MJNKRK,10-Oct-15,5,5
F01,Jan-00,LFIT MORSE TAPER HEAD,MKNN38,14-Nov-16,5,5
F01,Jan-05,LFIT MORSE TAPER HEAD,MJDV6X,8-Apr-15,5,5
F01,Jan-05,LFIT MORSE TAPER HEAD,MKAK94,22-Feb-16,5,5
Problem:
- First column data is converted from F01 to 1.
- I have tried with Access 12.0 drivers also.