March 2008
Intermediate to advanced
982 pages
22h 43m
English
You want to use ADO.NET to access data stored in a text file.
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.

Figure 1-15. Output for ConnectTextFile solution
The .NET OLE DB provider can read records from and insert records into a text file data source ...
Read now
Unlock full access