I wanna import csv file in c# windows application.
What I have tried:
StreamReader sr = new StreamReader(csvfolderPath);
string line = sr.ReadLine();
string[] value = line.Split(',');
DataTable dt = new DataTable();
DataRow row;
foreach (string dc in value)
{
dt.Columns.Add(new DataColumn(dc));
}
while (!sr.EndOfStream)
{
value = sr.ReadLine().Split(',');
if (value.Length == dt.Columns.Count)
{
row = dt.NewRow();
row.ItemArray = value;
dt.Rows.Add(row);
}
}
I am able to read the csv file from my above code but main problem is column have sentences with , separated and StreamReader is reading all column with comma separated so,
If I have column
"Column1" "column2" "column3" "column4" "My, Name"
Note: last column "My, Name" is single column
In above, I have 5 column
after result i am getting 5 columns like "Column1","column2","column3","column4","My","Name"
and if I split column with ,
then i am getting 5 column ("Column1","column2","column3","column4","My","Name")
so, can you explain me how to split column with two special character ",