Following Method to read data from csv file and stored into dataset
This method have one parameter which need to be passed full path of csv file.
For example
ReadCSV("c:\\mydata.csv");
This method have one parameter which need to be passed full path of csv file.
For example
ReadCSV("c:\\mydata.csv");
Method
using Microsoft.VisualBasic.FileIO; using System.Data; public DataSet ReadCSV(string csvfilePath) { var myDataset = new DataSet(); try { var csvData = new DataTable(); csvData.TableName = "TableName"; using (var csvReader = new TextFieldParser(csvfilePath, Encoding.Default)) { csvReader.SetDelimiters(new string[] { "," }); csvReader.HasFieldsEnclosedInQuotes = true; string[] colFields = csvReader.ReadFields(); if (colFields != null) foreach (string column in colFields) { var datecolumn = new DataColumn(column) { AllowDBNull = true }; csvData.Columns.Add(datecolumn); } while (!csvReader.EndOfData) { string[] fieldData = csvReader.ReadFields(); for (int i = 0; i < fieldData.Length; i++) { if (fieldData[i] == "") { fieldData[i] = null; } } csvData.Rows.Add(fieldData); } if (csvData.Rows.Count > 0) { myDataset.Tables.Add(csvData); } } return myDataset; } catch { return null; } }