CHAPTER 22 ADO.NET PART II: THE DISCONNECTED LAYER
maintain an internal strongly typed collection (named DataTableMappingCollection) of
System.Data.Common.DataTableMapping objects. You can access this collection using the TableMappings
property of your data adapter object.
If you so choose, you can manipulate this collection to inform a DataTable which display names it
should use when asked to print its contents. For example, assume that you want to map the table name
Inventory to Current Inventory for display purposes. For example, assume you want to display the
CarID column name as Car ID (note the extra space) and the PetName column name as Name of Car. To
do so, add the following code before calling the Fill() method of your data adapter object (be sure to
import the System.Data.Common namespace to gain the definition of the DataTableMapping type):
static void Main(string[] args)
{
...
// Now map DB column names to user-friendly names.
DataTableMapping custMap =
dAdapt.TableMappings.Add("Inventory", "Current Inventory");
custMap.ColumnMappings.Add("CarID", "Car ID");
custMap.ColumnMappings.Add("PetName", "Name of Car");
dAdapt.Fill(ds, "Inventory");
...
}
If you were to run this program again, you would find that the PrintDataSet() method now displays
the friendly names of the DataTable and DataRow objects, rather than the names established by the
database schema:
***** Fun with Data Adapters *****
DataSet is named: AutoLot
=> Current Inventory Table:
Car ID Make
Color
Name of Car
---------------------------------83
Ford
Rust
Rusty
107
Ford
Red
Snake
678
Yugo
Green
Clunker
904
VW
Black
Hank
1000
BMW
Black
Bimmer
1001
BMW
Tan
Daisy
1992
Saab
Pink
Pinkey
2003
Yugo
Rust
Mel
Source Code You can find the FillDataSetUsingSqlDataAdapter project under the Chapter 22 subdirectory.
891