Reputation: 13985
We have a text file with about 100,000 rows, about 50 columns per row, most of the data is pretty small (5 to 10 characters or numbers).
This is a pretty simple task, but just wondering what the best way would be to import this data into a C# data structure (for example a DataTable)?
Upvotes: 9
Views: 7314
Reputation: 53593
I would read it in as a CSV with the tab column delimiters:
Edit:
Here's a barebones example of what you'd need:
DataTable dt = new DataTable();
using (CsvReader csv = new CsvReader(new StreamReader(CSV_FULLNAME), false, '\t')) {
dt.Load(csv);
}
Where CSV_FULLNAME is the full path + filename of your tab delimited CSV.
Upvotes: 9
Reputation: 70317
Use .NET's built in text parser. It is free, has great error handling, and deals with a lot of odd ball cases.
http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser(VS.80).aspx
Upvotes: 3
Reputation: 5256
Simple, but not the necessarily a great way:
Read the file using a text reader into a string
Use String.Split to get the rows
use String.Split with a tab character to get field values
Upvotes: 0
Reputation: 10588
However you parse the lines, make sure you use something that supports forwarding and rewinding, being the data source of your data grid. You don't want to load everything into memory first, do you? How about if the amount of data should be ten-fold the next time? Make something that uses file.seek deep down, don't read everything to memory first. That's my advice.
Upvotes: 0
Reputation: 34592
What about FileHelpers, you can define the tab as a delimiter. HEad on over to that site by the link supplied and have a peeksy.
Hope this helps, Best regards, Tom.
Upvotes: 1
Reputation: 415880
Two options:
System.Data.OleDb
namespace. This has the advantage of reading directly into a datatable like you asked with very little code, but it can be tricky to get right because it's tab rather than comma delimited. Upvotes: 0