Tuesday, November 19, 2013

Data Import from CSV file to DataTable

             In this article, we will how to import CSV data to dot net DataTable with Stream and StreamReader in c#.

Using OpenFileDialog,

We can enable users to select files in a Silverlight-based application by using the OpenFileDialog class. After the user has selected one or more files, we can process the selected files.

Steps to include OpenFileDialog,

1. Create an instance of the OpenFileDialog class.
2. Optionally, specify the file filter string and the filter index by setting the Filter and FilterIndex properties.
3. Call the ShowDialog method to display the open file dialog box.
4. When the method returns, test whether the dialog box was closed with the OK button or the Cancel button. If it was closed with the OK button, process the user's selection.

Using StopWatch,

To keep track the elapsed time to import data from CSV to windows grid
1.  Import namespace System.Diagnostics
2. Create object for StopWatch Class and call start method.
3. Once the process complete Stop the Watch, find the time in ElapsedMilliseconds or in TimeSpan format.


Using Stream and StreamReader,

By using the stream and StreamReader we read data from the CSV file,
For more info about File Reading and StreamReader refer




Steps to create CSV file


1. Open Excel and type details in format like header followed by details required
2. In save dialog change Save as type to CSV (Comma delimited) and gave some file name.


Its saved as csv file, separted by comma(,) for each value in excel cell

Project Sample:



No comments:

Post a Comment