[ Team LiB ] Recipe 2.16 Mapping TableandColumnNamesBetweentheDataSourceand DataSet Problem You want to control thenames assigned to tables and columns when you fill a DataSet using a DataAdapter. Solution Use DataTableMapping and DataColumnMapping objects to map thenames of database tables and columns in thedatasource to different names in a DataSet when using a DataAdapter. The sample code defines a SQL statement to retrieve the CategoryID, CategoryName, and Description columns from the Categories table in Northwind. A DataAdapter is created with a DataTableMapping object to map the database table name Categories to the name tblmapCategories in the DataSet. Three DataColumnMapping objects are created to map the database columnnames to different names in thetable in the DataSet. The DataAdapter is used to fill a new DataSet. Finally, the default view of the mapped Categories table is bound to thedata grid on the form. The C# code is shown in Example 2-21 . Example 2-21. File: MappingsForm.cs // Namespaces, variables, and constants using System; using System.Configuration; using System.Data; using System.Data.Common; using System.Data.SqlClient; // . . . // Create the DataAdapter. String sqlText = "SELECT CategoryID, CategoryName, Description " + "FROM Categories"; SqlDataAdapter da = new SqlDataAdapter(sqlText, ConfigurationSettings.AppSettings["Sql_ConnectString"]); // Create thetablemapping to map the default table name 'Table'. DataTableMapping dtm = da.TableMappings.Add("Table", "tblmapCategories"); // Create thecolumn mappings for the Categories table. dtm.ColumnMappings.Add("CategoryID", "colmapCategoryID"); dtm.ColumnMappings.Add("CategoryName", "colmapCategoryName"); dtm.ColumnMappings.Add("Description", "colmapDescription"); // Create theDataSetand fill. DataSet ds = new DataSet( ); da.Fill(ds); // Retrieve and display the mapped name of thetable as grid caption. dataGrid.CaptionText = "TableName: " + ds.Tables[0].ToString( ); // Bind the default view of the Categories table to the grid. dataGrid.DataSource = ds.Tables["tblmapCategories"].DefaultView; Discussion When the Fill( ) method of the DataAdapter is used to fill a DataSet, thecolumnnames used in theDataSet default to thecolumnnames defined in thedata source. A DataAdapter has a collection of DataTableMapping objects in its DataTableMappingCollection accessed through its TableMappings property. These objects map the name of a table in thedatasource to a DataTable with different name in the DataSet. When a batch query is used to fill multiple tables within a DataSet, thetablenames default to Table, Table1, Table2, and so on. You can use tablemapping to rename tables created within theDataSet to match thetablenames in thedatasource or to map the tables returned from a batch query to DataTable objects that already exist within the DataSet. Each tablemapping object has a collection of DataColumnMapping objects in its DataColumnMappingCollection that are accessed through its ColumnMappings property. These objects map the name of a column in thedatasource to a column with a different name in theDataSet for thetable associated with the containing tablemapping object. The Fill( ) method of the DataAdapter always uses mapping information (if present) to retrieve data from a data source. The FillSchema( ) method accepts an argument specifying whether to use mapping information when retrieving schema information from a data source. Like the Fill( ) method, the Update( ) method always uses mapping information (if present) when submitting DataSet changes back to thedata source. In the solution, the Categories table retrieved by the query is mapped to a table in theDataSet called tblmapCategories with the following code: DataTableMapping dtm = da.TableMappings.Add("Table", "tblmapCategories"); Without thetable mapping, a table named Table will be created when the Fill( ) method is called. For a query returning a single table, thetablemapping can also be specified by using an overload of the Fill( ) method as shown: da.Fill(ds, "tblmapCategories"); The solution also maps the three columnnames returned by the query, CategoryID, CategoryName, and Description using the following code: dtm.ColumnMappings.Add("CategoryID", "colmapCategoryID"); dtm.ColumnMappings.Add("CategoryName", "colmapCategoryName"); dtm.ColumnMappings.Add("Description", "colmapDescription"); Thecolumnmapping objects are added to thetablemapping object for thetable containing the columns to be mapped. [ Team LiB ] . a DataSet using a DataAdapter. Solution Use DataTableMapping and DataColumnMapping objects to map the names of database tables and columns in the data source. tblmapCategories in the DataSet. Three DataColumnMapping objects are created to map the database column names to different names in the table in the DataSet. The DataAdapter