September 3, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Managed Extensions: Using the .NET ODBC Classes to Read Text Data

  • September 14, 2004
  • By Tom Archer
  • Send Email »
  • More Articles »

Taking Control of the Process with the schema.ini File

Once you've started working with text files via a DSN-less connection, you might run into situations that will have you asking things like "How do I specify how the file is delimited (e.g., tab vs. comma)?" or "Where can I specify the character set?" These settings and more can be specified via a very simple file named schema.ini that resides in the same directory as the data file. The schema.ini file is documented on the Microsoft Web site so I won't attempt to cover every possible parameter that can be specified. However, I will cover the most popular question I see on the Internet: how to specify if the data includes (as its first row) the column names of the data.

By default, the text driver assumes that the data contains a column heading row. Therefore, if your data does not contain this row and you do not define a schema.ini file, you will find that the first row of data being ignored. For example, if your data looked like the following, the reader code above would display only the second and third records (leaving out your favorite author!):

Tom Archer,Archer Consulting Group
Bradley Jones,Jupitermedia
Bill Gates,Microsoft
In order to specify that the data does not include a column row and that you don't wish to name the columns, your schema.ini file would look like the following:

data file

[data.txt]
ColNameHeader=FALSE
In terms of specifying the column names for your data, you have two choice:
  • You can include—as the first row in the text file—the column names and then specify the ColNameHeader attribute in the schema.ini file. (You can also omit the schema.ini file, as the text driver defaults the ColNameHeader value to TRUE).

    data file
    Name,Company
    Tom Archer,Archer Consulting Group
    Bradley Jones,Jupitermedia
    Bill Gates,Microsoft
    
    schema.ini file
    [data.txt]
    ColNameHeader=TRUE
    
  • If the data doesn't include a column row, you can manually set the column names such that the two files look as follows:

    data file

    Tom Archer,Archer Consulting Group
    Bradley Jones,Jupitermedia
    Bill Gates,Microsoft
    
    schema.ini file
    [data.txt]
    ColNameHeader=FALSE
    Col1=Name Char Width 255
    Col2=Company Char Width 255
    
The column name can be retrieved from the reader using the OdbcDataReader::GetName method. This article's demo application uses the last technique and—while being very simple in scope—allows you to tinker with your data file and schema.ini file so that you can easily test the various configuration combinations until you get it right for your particular application.

Download the Code

To download the accompanying source code for this article, click here.

About the Author

The founder of the Archer Consulting Group (ACG), Tom Archer has been the project lead on three award-winning applications and is a best-selling author of 10 programming books as well as countless magazine and online articles.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel