Tornado Class Library

Special Topic - Csv & Text File

CSV and TEXT file data source is different than Database sources. DSN should be set as the virtual or physical path of the target directory file (w/o the filename) and dbSQL should Select from the text source. See the following example -

 

Example - Basic Csv Access
<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
  Dim EM As New Tornado.z
  With EM
    .dbQP = "U=1| S=1|PS=-1| Ni=none| Q=Select * From Email.Csv| Dt=TEXT| Pv=OLEDB"
    .dbDSN = "/tornado/DB"
    .ASPdbNET()
  End With
End Sub
</script> 

The alternative is to specify the full dbDSN without taking the ASP-db short cut. In this case, dbProvider still need to be specify to match the user specified dbDSN.

 

Example - User Specified DSN and Odbc provider
<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
  Dim TXT As New Tornado.z
    With TXT
      .dbQP = "U=99| S=7| Ps=-1| Pv=ODBC| Dt=TEXT| ni=none| th=t=TEXT-ODBC"
      .dbDSN = "Driver={Microsoft Text Driver (*.txt; *.csv)}; DBQ=" & Server.MapPath("/tornado/DB")
      .dbSQL = "Select * From tabtext.txt"
      .ASPdbNET()
    End With
End Sub
</script> 

 

Example - User Specified DSN and OleDb provider
<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
  Dim EM As New Tornado.z
  With EM
    .dbQP = "U=1| S=1|PS=-1| Ni=none| Q=Email.Csv| Dt=TEXT| Pv=OLEDB"
    .dbDSN = "Provider=Microsoft.Jet.OLEDB.4.0; Extended Properties=Text; Data Source=" & Server.MapPath("/tornado/DB")
    .ASPdbNET()
  E nd With
End Sub
</script> 

 

Example - Read a tab delimited TEXT file as data source

First we create a tab delimited text file -

File= c:\inetpub\wwwroot\Tornado\DB\Tabtext.txt

Frank	Kwong	123
John	Doe	456
Bill	Clinton 	789

Next we create a Schema.ini file in the same directory of the data file -

File= c:\inetpub\wwwroot\Tornado\DB\Schema.ini

[tabtext.txt]
ColNameHeader = False
Format = TabDelimited
CharacterSet = ANSI
Col1=FirstName char width 10
Col2=LastName char width 10
Col3=ID char width 10

Finally we create the aspx file to read the tab delimited text file as data source

File= c:\inetpub\wwwroot\Tornado\ReadTabText.aspx

<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
  Dim TXT As New Tornado.z
    With TXT
      .dbQP = "U=99|S=7|M=Grid|Ps=-1|Pv=ODBC|ni=none|th=t=TEXT-ODBC"
      .dbDSN = "Driver={Microsoft Text Driver (*.txt; *.csv)}; DBQ=" & Server.MapPath("/tornado/DB")
      .dbSQL = "Select * From tabtext.txt"
      .dbType = "TEXT"
      .ASPdbNET()
    End With
End Sub
</script> 

 

Schema.ini File Reference

When the Text driver is used, the format of the text file is determined by using a schema information file. The schema information file, which is always named Schema.ini and always kept in the same directory as the text data source, provides the IISAM with information about the general format of the file, the column name and data type information, and a number of other data characteristics. A Schema.ini file is always required for accessing fixed-length data; you should use a Schema.ini file when your text table contains DateTime, Currency, or Decimal data or any time you want more control over the handling of the data in the table.

Note:   The Text ISAM will obtain initial values from the registry, not from Schema.ini. The same default file format applies to all new text data tables. All files created by the CREATE TABLE statement inherit those same default format values, which are set by selecting file format values in the Define Text Format dialog box with <default> chosen in the Tables list. If the values in the registry are different from the values in Schema.ini, the values in the registry will be overwritten by the values from Schema.ini.

 

Understanding Schema.ini Files

Schema.ini files provide schema information about the records in a text file. Each Schema.ini entry specifies one of five characteristics of the table:

The following sections discuss these characteristics.

Specifying the File Name

The first entry in Schema.ini is always the name of the text source file enclosed in square brackets. The following example illustrates the entry for the file Sample.txt:

[Sample.txt]

 

Specifying the File Format

The Format option in Schema.ini specifies the format of the text file. The Text IISAM can read the format automatically from most character-delimited files. You can use any single character as a delimiter in the file except the double quotation mark ("). The Format setting in Schema.ini overrides the setting in the Windows Registry on a file-by-file basis. The following table lists the valid values for the Format option.

Format specifier Table format Schema.ini Format statement
Tab Delimited Fields in the file are delimited by tabs. Format=TabDelimited
CSV Delimited Fields in the file are delimited by commas (comma-separated values). Format=CSVDelimited
Custom Delimited Fields in the file are delimited by any character you choose to input into the dialog box. All except the double quote (") are allowed, including blank. Format=Delimited(custom character)

-or-

With no delimiter specified:

Format=Delimited( )

Fixed Length Fields in the file are of a fixed length. Format=FixedLength

 

Specifying the Fields

You can specify field names in a character-delimited text file in two ways:

You must specify each column by number and designate the column name, data type, and width for fixed-length files.

Note   The ColNameHeader setting in Schema.ini overrides the FirstRowHasNames setting in the Windows Registry on a file-by-file basis.

The data types of the fields can also be determined. Use the MaxScanRows option to indicate how many rows should be scanned when determining the column types. If you set MaxScanRows to 0, the entire file is scanned. The MaxScanRows setting in Schema.ini overrides the setting in the Windows Registry on a file-by-file basis.

The following entry indicates that Microsoft Jet should use the data in the first row of the table to determine field names and should examine the entire file to determine the data types used:

ColNameHeader=True
MaxScanRows=0

The next entry designates fields in a table by using the column number (Coln) option, which is optional for character-delimited files and required for fixed-length files. The example shows the Schema.ini entries for two fields, a 10-character CustomerNumber text field and a 30-character CustomerName text field:

Col1=CustomerNumber Text Width 10
Col2=CustomerName Text Width 30

The syntax of Coln is:

Coln=ColumnName type [Width #]

The following table describes each part of the Coln entry.

Parameter Description
ColumnName The text name of the column. If the column name contains embedded spaces, you must enclose it in double quotation marks.
type Data types are:

Microsoft Jet data types
Bit
Byte
Short
Long
Currency
Single
Double
DateTime
Text
Memo

ODBC data types
Char (same as Text)
Float (same as Double)
Integer (same as Short)
LongChar (same as Memo)
Date date format

Width The literal string value Width. Indicates that the following number designates the width of the column (optional for character-delimited files; required for fixed-length files).
# The integer value that designates the width of the column (required if Width is specified).

Selecting a Character Set

You can select from two character sets: ANSI and OEM. The CharacterSet setting in Schema.ini overrides the setting in the Windows Registry on a file-by-file basis. The following example shows the Schema.ini entry that sets the character set to ANSI:

CharacterSet=ANSI

Specifying Data Type Formats and Conversions

The Schema.ini file contains a number of options that you can use to specify how data is converted or displayed. The following table lists each of these options.

Option Description
DateTimeFormat Can be set to a format string indicating dates and times. You should specify this entry if all date/time fields in the import/export are handled with the same format. All Microsoft Jet formats except A.M. and P.M. are supported. In the absence of a format string, the Windows Control Panel short date picture and time options are used.
DecimalSymbol Can be set to any single character that is used to separate the integer from the fractional part of a number.
NumberDigits Indicates the number of decimal digits in the fractional portion of a number.
NumberLeadingZeros Specifies whether a decimal value less than 1 and greater than �1 should contain leading zeros; this value can either be False (no leading zeros) or True.
CurrencySymbol Indicates the currency symbol to be used for currency values in the text file. Examples include the dollar sign ($) and Dm.
CurrencyPosFormat Can be set to any of the following values:
  • Currency symbol prefix with no separation ($1)
  • Currency symbol suffix with no separation (1$)
  • Currency symbol prefix with one character separation ($ 1)
  • Currency symbol suffix with one character separation (1 $)
CurrencyDigits Specifies the number of digits used for the fractional part of a currency amount.
CurrencyNegFormat Can be one of the following values:
  • ($1)
  • ?$1
  • $?1
  • $1?
  • (1$)
  • ?1$
  • 1?$
  • 1$?
  • ?1 $
  • ?$ 1
  • 1 $?
  • $ 1?
  • $ ?1
  • 1? $
  • ($ 1)
  • (1 $)

This example shows the dollar sign, but you should replace it with the appropriate CurrencySymbol value in the actual program.

CurrencyThousandSymbol Indicates the single-character symbol to be used for separating currency values in the text file by thousands.
CurrencyDecimalSymbol Can be set to any single character that is used to separate the whole from the fractional part of a currency amount.
Note:   If you omit an entry, the default value in the Windows Control Panel is used.

 

 


Send comments on this topic.
Copyright 1998-2006 ASP-db