Click here to Skip to main content
13,046,087 members (58,506 online)
Click here to Skip to main content
Add your own
alternative version

Stats

13.2K views
21 bookmarked
Posted 31 Oct 2010

DataImporter, A Component to Easily Read Data from Files

, 31 Oct 2010
Rate this:
Please Sign up or sign in to vote.
Component to load data from files or DB connection, gets the data asynchronously and fires an event when it finishes. All the read data is loaded to a DataTable
01.PNG

Contents

Introduction

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.

Background

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.

Using the Code

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.

Component's Properties

  • 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).

Component's Events

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.

Points of Interest

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.

Conclusion

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.

History

  • October 29, 2010: First release

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

TG_Cid
Mexico Mexico
I have been working as a profesional developer since June 2008, since then i learnt to program in C# with Visual Studio 2005, before that i used to develop on Borland Builder C++ and some Java IDE's.

You may also be interested in...

Comments and Discussions

 
QuestionEl código? Pin
framirez@icd.go.cr2-Nov-10 5:13
memberframirez@icd.go.cr2-Nov-10 5:13 
AnswerRe: El código? Pin
TG_Cid2-Nov-10 5:28
memberTG_Cid2-Nov-10 5:28 
GeneralMy vote of 3 Pin
Toli Cuturicu1-Nov-10 10:43
memberToli Cuturicu1-Nov-10 10:43 
GeneralRe: My vote of 3 Pin
TG_Cid2-Nov-10 5:33
memberTG_Cid2-Nov-10 5:33 
GeneralMy vote of 3 Pin
Sacha Barber31-Oct-10 8:22
mvpSacha Barber31-Oct-10 8:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 31 Oct 2010
Article Copyright 2010 by TG_Cid
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid