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



Shawn
Shawn Wildermuth
Author, Teacher, and Coach




My Courses

Wilder Minds Training
Vue.js by Example (New Lower Price)
Bootstrap 4 by Example (New Lower Price)
Intro to Font Awesome 5 (Free Course)
Pluralsight
Building an API with ASP.NET Core (New Course)
Building a Web App with ASP.NET Core, MVC6, EF Core, Bootstrap and Angular (updated for 2.2)
Less: Getting Started (New)
Using Visual Studio Code for ASP.NET Core Projects
Implementing ASP.NET Web API

Application Name WilderBlog Environment Name Production
Application Ver v4.0.30319 Runtime Framework x86
App Path D:\home\site\wwwroot\ Runtime Version .NET Core 4.6.27514.02
Operating System Microsoft Windows 10.0.14393 Runtime Arch X86