<!-- 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 (
IniFileReader creating the corresponding rows and adding them to the
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
The following code shows the declaration of delegates and events in the
public delegate void SectionHandler(string sectionName);
public event SectionHandler SectionRead;
public delegate void KeyValueHandler(string section, <BR> string key, string sValue);
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.
public void ReadIniFile(string filename)
StreamReader sr = new StreamReader(filename);
while (not end of file)
if (oneLine.StartsWith("[") && oneLine.EndsWith("]"))
KeyValueRead(currentSection, keyvalue, keyvalue);
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.
public void ReadIniFile(string PathName)
IniFileReader ir = new IniFileReader(); <BR>
ir.SectionRead += new IniFileReader.SectionHandler(<BR> this.OnSectionRead);
ir.KeyValueRead += new IniFileReader.KeyValueHandler(<BR> this.OnKeyValueRead);
private void OnSectionRead(string Section)
private void OnKeyValueRead(string Section, string Key, <BR> string sValue)
Points of Interest
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.