Raise your hand if you know what DataAdapter.TableMappings is for...

I seem to be getting a lot of questions and reviewing a lot of code that isn't use TableMappings and I wonder why.  For example I see this occassionally:

 DataSet ds = new DataSet(); 
SqlDataAdapter da = new SqlDataAdapter();
// Setup Adapter here ds.Fill(ds);

ds.Tables["Table"].TableName = "Customers";
//etc.

The problem here is that you don't even need TableMappings, you could call the fill like so:

ds.Fill(ds, "Customers");

The problem really presents itself when you're returning multiple resultsets because the fill won't take multiple names. The solution is TableMappings:

 DataSet ds = new DataSet(); 
SqlDataAdapter da = new SqlDataAdapter();
// Setup Adapter here

// Setup a mapping for each resultset
da.TableMappings("Table", "Customers");
da.TableMappings("Table1", "Orders");
da.TableMappings("Table2", "Order Details");
da.TableMappings("Table3", "Products");

ds.Fill(ds);

Lastly, this is especially important because if you use Typed DataSets you need to fill the Tables that already exist in the Typed DataSet.

HTH

Comments:

(raising both hands and legs)

Table mappings can be useful in certain circumtances.

If you have multiple result sets for the same db table (pulling queries for, say, five windows) into tablexxx_1, tablexxx_2 and tablexxx_3.

When you go to write changes to these tables back to the db and want to use a tool such as the sql command builder then you need the table mapping so that it correctly writes a 'Update into tablexxx' and not 'update into tablexxx_1'.

I'm sure there are other uses.

Ian


 



 
Save Cancel