Shawn Wildermuth

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


Url: http://msdn.microsoft.com/library/default.asp?u...

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