September 2003
Intermediate to advanced
624 pages
14h 27m
English
You want to use ADO.NET to access data stored in a text file.
Use the OLE DB Jet provider to access data in a text file.
The sample code creates an OleDbDataAdapter that
uses the Jet OLE DB provider to load the contents of the text file
Categories.txt, shown in Example 1-13, into a DataTable and
displays the contents in a data grid on the form.
Example 1-13. File: Categories.txt
"CategoryID","CategoryName","Description" 1,"Beverages","Soft drinks, coffees, teas, beers, and ales" 2,"Condiments","Sweet and savory sauces, relishes, spreads, and seasonings" 3,"Confections","Desserts, candies, and sweet breads" 4,"Dairy Products","Cheeses" 5,"Grains/Cereals","Breads, crackers, pasta, and cereal" 6,"Meat/Poultry","Prepared meats" 7,"Produce","Dried fruit and bean curd" 8,"Seafood","Seaweed and fish"
The C# code is shown in Example 1-14.
Example 1-14. File: ConnectTextFileForm.cs
// Namespaces, variables, and constants
using System;
using System.Configuration;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
// . . .
// Create the data adapter to retrieve all rows from text file.
OleDbDataAdapter da =
new OleDbDataAdapter("SELECT * FROM [Categories.txt]",
ConfigurationSettings.AppSettings["TextFile_0119_ConnectString"]);
// Create and fill the table.
DataTable dt = new DataTable("Categories");
da.Fill(dt);
// Bind the default view of the table to the grid.
categoriesDataGrid.DataSource = dt.DefaultView;The Jet ...
Read now
Unlock full access