Use the OLE DB .NET data provider.
The solution creates and opens a connection to a text file using the OLE DB .NET data provider. Information about the connection is displayed.
The C# code in Program.cs in the project ConnectTextFile
is shown in Example 1-15.
Example 1-15. File: Program.cs for ConnectTextFile solution
using System; using System.Data; using System.Data.OleDb; namespace ConnectTextFile { class Program { static void Main(string[] args) { string connectString = "Provider=Microsoft.ACE.OLEDB.12.0;" + @"Data Source=..\..\..\;" + "Extended Properties=\"text;HDR=yes;FMT=Delimited\";"; OleDb Connection connection = new OleDbConnection(connectString); connection.Open( ); // Output some connection properties to the console Console.WriteLine("Connection.String = {0}\n", connectString); Console.WriteLine("Connection.State = {0}", connection.State); Console.WriteLine("Connection.Provider = {0}", connection.Provider); Console.WriteLine("Connection.ServerVersion = {0}", connection.ServerVersion); connection.Close( ); Console.WriteLine("\nPress any key to continue."); Console.ReadKey( ); } } }
The output is shown in Figure 1-15.
The .NET OLE DB provider can read records from and insert records into a text file data source using the Microsoft Access database engine (ACE) driver. The ACE driver can access other database file formats through Indexed Sequential Access Method (ISAM) drivers specified in the Extended Properties
attribute of the connection. Text files are supported with the text
source database type as shown in the following example:
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\..\..\; Extended Properties="text;HDR=yes;FMT=Delimited";
Notice that only the directory for the text file is specified in the connection string. The filename of the text file is specified in the T-SQL commands that access data in the text file, similar to a table name in a database.
The Extended Properties
attribute can, in addition to the ISAM version property, specify whether tables include headers as field names in the first row of a range using an HDR
attribute.
It is not possible to define all characteristics of a text file through the connection string. You can access files that use nonstandard text delimiters and fixed-width text files by creating a schema.ini file in the same directory as the text file. A text file, Category.txt, is shown in Figure 1-16.
A possible schema.ini file for the Category.txt file is:
[Category.txt] Format=CSVDelimited ColNameHeader=True MaxScanRows=0 Character=OEM Col1=CategoryID Long Width 4 Col2=CategoryName Text Width 15 Col3=Description Text Width 100
The schema.ini file provides this schema information about the data in the text file:
Filename
File format
Field names, widths, and data types
Character set
Special data type conversions
The first entry in the schema.ini file is the text filename enclosed in square brackets. For example:
[Category.txt]
The Format
option specifies the text file format. Table 1-2 describes the options.
Table 1-2. Schema.ini format options
You can specify the fields in the text file in two ways:
The MaxScanRows
option indicates how many rows should be scanned to automatically determine column type. A value of 0 indicates that all rows should be scanned.
The ColN
entries specify the name, width, and data type for each column. This entry is required for fixed-length formats and optional for character-delimited formats. The syntax of the ColN
entry is:
ColN=columnName dataType
[Widthn
]
The parameters in the entry are:
columnName
The name of the column. If the column name contains spaces, it must be enclosed in double quotation marks.
dataType
The data type of the column. This value can be
Bit, Byte, Currency, DateTime, Double, Long, Memo, Short, Single,
orText
.DateTime
values must be in one of the following formats:dd-mmm-yy, mm-dd-yy, mmm-dd-yy, yyyy-mm-dd,
oryyyy-mmm-dd,
wheremm
is the month number andmmm
are the characters specifying the month.Width
n
The literal value
Width
followed by the integer value specifying the column width.
The Character
option specifies the character set; you can set it to either ANSI
or OEM
.
Get ADO.NET 3.5 Cookbook, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.