Contents
First of all, let me introduce myself. I am a Mexican programmer (so I apologize in advance if my English is not completely understandable) and I enjoy programming in "C#" with "Visual Studio" or "Sharp Develop."
It has been a little over 2 years now since I found this site ("CodeProject"), and it has been very useful in several projects, so I always wanted to contribute something to this community.
A few weeks ago, I got a new requirement for a system already in production. This new requirement involved the reading of an XLS file. In previous projects, I had already implemented modules which read XLS files. So I searched my code, copied it and modified it to fit this new requirement. But I realized that this was not as easy as expected (I mean it was easy, but it could be easier), so it did a great candidate for a new component, and because it is kind of simple, it is also a great candidate for my first article on "CodeProject".
With this in mind, I decided to make a component for reading information from a file and save the information in a "DataTable
" for later usage, but did not want to limit the component to the reading of information to a single file type. The component should be configurable to accept a reading of some file types.
Originally the file types were:
- XLS file
- Plain Text File
- Fixed Length Fields
- Character Separated Fields
- Character Separator: Tab (for convenience)
- Character Separator: Any
Once finished this control, with the above requirements, I thought it was good to add one more functionality, i.e. I could get information from not only files but also from a database (I will explain the reason in the section to be done). So I added one more option.
Database
Then finally, after this last improvement, I thought that a very good idea would be also to add a progress window integrated to the component, and make the whole reading process in a different thread.
This control can be used like any other control, i.e., if you add the reference to the tool box, you should see the component ready to drag and drop on your window. Once inside your window, you can configure it with the "PropertyGrid
" Visual Studio.
-
public bool FileHeader
Boolean property, defines if the file to read will have a header.
-
public bool FileHeader
Boolean property, defines if the file to read will have a header.
-
public string FileName
Path and name of the source file.
-
public char FillerChar
Char
used to fill a field, when the data is smaller than the fixed length for that field.
-
public DbConnection LoadConnection
Read only property, connection component used to retrieve data from the DB.
-
public bool PercentAdvance
Defines if the progress windows should show only percents.
-
public string Query
SQL Query to be executed to retrieve the data from the DB.
-
public string ReadingString
Text shown in the progress window, when reading the file.
-
public string ReadingTitleString
Text shown as title in the progress window, when reading the file. The constant "@FileName
" is always replaced with filename of the file that is being read.
-
public bool Running
Readonly boolean property, true
if the component is working (i.e., reading a file/loading data).
-
public bool RunningAndVisible
Readonly boolean property, true
if the component is working (i.e., reading a file/loading data) and the progress window is visible.
-
public Delimiter Separator
Enum
property, defines the Kind of separators the file will have.
-
public char SeparatorChar
Character used in the file as delimiter.
-
public ImportType Source
Enum
property, defines the kind of source from which the data will be retrieved.
-
public int XLS_PageNumber
Defines the number of the page to read, in case the source file is a .xls file (zero based index).
The component has 3 events which are:
-
void StartLoad(DateTime StartTime, int Rows)
Triggered before reading the file or database.
-
void EndLoad(DateTime StartTime, int Rows)
Triggered after reading the file or database.
-
void PercentChange(Currow int, int TtRow)
Triggered when reading the current record changes the total percentage of reading (integer).
Note: If reading from an XLS file where the component does not know the number of lines this event will never be fired, just as it will not be triggered by a reading of a database.
The reading of XLS files is done using Excel automation, therefore the speed of this reading is not very good.
Connecting to a database is done through a System.Data.Common.DbConnection
, there are two ways to set it in the control:
-
SetLoadConnection(System.Data.Common.DbConnection Conx)
Setting the connection "manually", i.e. passing the connection to the component.
-
SetLoadConnection()
Prompting the user to set the connection with the connection dialog that Microsoft provides for freely (http://code.msdn.microsoft.com/Connection).
The option of reading a database to a DataTable
does not look interesting at first, but I included it because I am thinking about developing another component that acts as a complement to this, a DataExporter
, i.e., to take information from a DataTable
and export it to a file or upload it to a DB.
The article is perhaps a little short, due to my difficulty with English and also it is my first posting, but I hope that the component shown here is useful and that the example is sufficiently explicit to clarify all your doubts.
- October 29, 2010: First release