Click here to Skip to main content
11,927,711 members (54,007 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked

A simple architecture to read arbitrarily formatted flat files into ADO DataSets

, 25 Apr 2003
Rate this:
Please Sign up or sign in to vote.
This article gives a simple architecture of using StreamReaders and DataSets together to read data from arbitrarily formatted flat files into DataSets, using ini files as an example
<!-- Add the rest of your HTML here -->


There are Ole Providers to read formatted flat files into DataSets, but ever wondered about importing data from files which are not simply comma delimited or tab delimited (not columnar) but some native format.

One of the simplest examples I could come up with are ini files. Although simple enough, there is no columnar or delimited data. So how do we use C# to import such files into a database ?

Since there are 2 distinct pieces of data (sections and key/value pairs) the a reader class uses a System.IO.StreamReader to read lines from the specified ini file and raise events when a section or a key/value pair is encountered. A data class, the IniFileData class has a System.Data.DataSet member holding all the sections, keys and values from the ini file. This class subscribes to the events (SectionRead and KeyValueRead) from IniFileReader creating the corresponding rows and adding them to the DataTables.

Once the data is read into the DataSet it is left to the user's interest to deal with it. I have given a small example by writing the GetValue() method on the IniFileData class to retrive a value by giving the section and key name.

Using the code

Create an instance of IniFileData class and give an ini file name. The class reads the file and prepares the DataSet.

The following code shows the declaration of delegates and events in the IniFileReader.

// delegate to receive a SectionRead event
public delegate void SectionHandler(string sectionName);
// event raised when a section is read
public event SectionHandler SectionRead;
// delegate to receive a KeyValueRead event
public delegate void KeyValueHandler(string section, 
    string key, string sValue);
// event raised when a key, value pair is read
public event KeyValueHandler KeyValueRead;

These events are kicked off as the StreamReader is reading the flat file. The most appropriate way to kick off these events is when a row of data is available in any table in the DataSet. Here you can see that these events are thrown when a section is read or a key/value pair is read.

class IniFileReader
{    .......
    public void ReadIniFile(string filename)
        StreamReader sr = new StreamReader(filename);
        while (not end of file)
            if (oneLine.StartsWith("[") && oneLine.EndsWith("]"))
                        // raise the section read event
            else {
                // raise an event if a key/value pair is read
                KeyValueRead(currentSection, keyvalue[0], keyvalue[1]);

The class which is preparing holding the DataSet, in this case the IniFileData class subscribes to these events and prepares the rows and inserts them into the corresponding tables. The relevant code is pointed below.

class IniFileData
    public void ReadIniFile(string PathName)
        IniFileReader ir = new IniFileReader(); 
               // create instance of iniFileReader

        // subscribe to the event handlers
        ir.SectionRead += new IniFileReader.SectionHandler(
        ir.KeyValueRead += new IniFileReader.KeyValueHandler(
    private void OnSectionRead(string Section)
        // create the sections here
    private void OnKeyValueRead(string Section, string Key, 
               string sValue)
        // insert the key value pairs here

Points of Interest

In the IniFileData.GetValue() method I couldn't find a way to join different tables in a single query when retreiving the value. If I find one I will post it here.  


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Sriram Chitturi
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Generalsupport for ini file in-line comments Pin
mcmintexas21-Jul-03 13:35
membermcmintexas21-Jul-03 13:35 
GeneralStrip comments Pin
Todd Beaulieu18-Apr-07 5:21
memberTodd Beaulieu18-Apr-07 5:21 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 26 Apr 2003
Article Copyright 2003 by Sriram Chitturi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid