Hierarchical Datasets Updates
As a part of a session that
I’m suppose to deliver on March,
I’m going to explain the
datasets enhancements that
came along with Visual Studio 2008.
In particularly, I’m going to explain what is the TableAdapterManager and
how it is going to help you with hierarchical datasets updates. The post
is going to explain that subject as well.
The TableAdapterManager Class
When you create a new typed-dataset in Visual Studio 2008 by default a class
by the name TableAdapterManager is generated. The main purpose of the class
is to enable the functionality to save data in related data tables. The
TableAdapterManager uses the relations between the data tables in order to
determine in which order to perform a set of CRUD operations without violating
any foreign-key constraints. This behavior is called hierarchical update. This new
behavior is suppose to free the developers from the plumbing of building
CRUD scenarios by themselves when using datasets. The hierarchical update behavior
is best suited to Master-Details forms. As a backward compatibility a new property
with the name Hierarchical Update was added to the typed-dataset. That property is
by default True but in datasets that were added to projects that were created in
earlier versions of Visual Studio the Hierarchical Update property is set to False.
How to Perform Hierarchical Datasets Updates
Lets generate the dataset. I’ve created a class library that is called
DataAccessLayer. In that library I generated a new typed-dataset
with the name SchoolDataSet. Then, I dragged the Course and
Department tables to the designer. The following image shows the
outcome of the operations I described:
As you can see in the image I put the mouse pointer on the new
Hierarchical Update property. If you don’t want to use that feature
change the property to False.
The following class provide a method to update Courses and Departments
in hierarchical update:
In order to use hierarchical update we need to create a new
TableAdapterManager and insert the relevant table adapters that will
participate in the update. The reason for that is obvious. If we have a lot
of data tables in the dataset that don’t participate in the update, why should
we build adapters for them? it will be a waist of memory. After building the
adapters all we have to do is to call the UpdateAll method that perform the
hierarchical update. Simple as that.
Lets sum up, Visual Studio 2008 brought with it some enhancements for the
datasets world. One enhancement is the hierarchical update feature that enables
to update a set of data tables that relate to one another without the need to
indicate the order of operations. I showed how to use the TableAdapterManager
which is the base for the hierarchical update behavior.