Bohdan StupakRank #479 · 5.2K reputationPosted Nov 12, 2021, 2:44 PM EST
+2
In my opinion the best one is CsvHelper. We use it in production to load files with 2M rows. The most useful features are ability to add field mappings and ability to load rows as IEnumerable so big files are not loaded in memory instantly but can be read row by row
+2
Sachin SinghRank #66 · 26.2K reputationPosted Nov 13, 2021, 1:57 AM EST
+2
Sylvan.Data.Csv is also a good choice and performance is slightly better than CsvHelper, however both are good.
Here is a peace of program code I have used for reading CSV dokument containig values stored from table 1010470 rows by 16 columns. Values are read from document and stored to Table type of string for furder use.
Document size is ~91,2 MB.
Average read / store time is 3,1 second.
It is about 10% faster than same program written using CSVHelper.
Faster than this is possible only if I upgrade hardware on my computer with faster HD drive as SSD drive, faster processor, etc.
using System;
using System.IO;
using LumenWorks.Framework.IO;
namespace CSV_Reader
{
class Program
{
publicstaticvoid Main(string[] args)
{
conststring CSV_File_Path = "Q3.csv";
StreamReader stream = new StreamReader(System.IO.File.OpenRead(CSV_File_Path));
var LumenWorks_Reader = new LumenWorks.Framework.IO.Csv.CsvReader(stream, false);
string [,] Table = newstring[1100000, 20];
int row = 0;
while (LumenWorks_Reader.ReadNextRecord())
{
Table[row, 0] = LumenWorks_Reader[0];
Table[row, 1] = LumenWorks_Reader[1];
Table[row, 2] = LumenWorks_Reader[2];
Table[row, 3] = LumenWorks_Reader[3];
Table[row, 4] = LumenWorks_Reader[4];
Table[row, 5] = LumenWorks_Reader[5];
Table[row, 6] = LumenWorks_Reader[6];
Table[row, 7] = LumenWorks_Reader[7];
Table[row, 8] = LumenWorks_Reader[8];
Table[row, 9] = LumenWorks_Reader[9];
Table[row, 10] = LumenWorks_Reader[10];
Table[row, 11] = LumenWorks_Reader[11];
Table[row, 12] = LumenWorks_Reader[12];
Table[row, 13] = LumenWorks_Reader[13];
Table[row, 14] = LumenWorks_Reader[14];
Table[row, 15] = LumenWorks_Reader[15];
row++;
}
stream.Close();
stream.Dispose();
LumenWorks_Reader.Dispose();
Console.WriteLine();
Console.WriteLine("Press any key to continue . . . ");
satheesh d190 reputationPosted Nov 26, 2021, 4:59 AM EST
+1
In C#, we can use the Nuget package manager in order to load the data from the CSV file to a data table.We can use Lumen CSV Reader package to read the CSV file in C#. We can instal this package in Nuget Package Manager in Visual Studio.
After that we need to add the namespace for the reference of this package.Using LumenWorks.Framework.IO.Csv;
Using LumenWorks.Framework.IO.Csv;
var NewTable = new DataTable();
using (var csvReader = new CsvReader(new StreamReader(System.IO.File.OpenRead(@"File path")), true))
{
NewTable.Load(csvReader);
}
We can create the model properties ie model class for the CSV file. then after extracting data from CSV to data table, we need to add all the rows to th list with respect to propery binding.
There are other methods like CSV Helper, Sylvan are also used to read the CSV files in C#.
Srinivasan RamamoorthiRank #877 · 2.5K reputationPosted Nov 12, 2021, 11:15 PM EST
You can check this article
https://www.c-sharpcorner.com/article/how-to-read-data-from-csv-file-in-c-sharp/
Bohdan StupakRank #479 · 5.2K reputationPosted Nov 12, 2021, 2:44 PM EST
Sachin SinghRank #66 · 26.2K reputationPosted Nov 13, 2021, 1:57 AM EST
geetha geethaRank #7,130 · 248 reputationPosted Nov 25, 2021, 9:45 AM EST
Željko PerićRank #644 · 3.7K reputationPosted Nov 25, 2021, 12:32 PM EST
satheesh d190 reputationPosted Nov 26, 2021, 4:59 AM EST
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts