Hierarchical Datasets Updates

January 23, 2009

Hierarchical Datasets Updates

As a part of a session that  Hierarchical Datasets Updates
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

The following example is going to use the typed-dataset that is
generated from the following database schema:
School Database Diagram

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:
SchoolDataSet Designer

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:

public class DataProvider
{
    public void UpdateCoursesAndDepartments(SchoolDataSet ds)
    {
        TableAdapterManager manager = new TableAdapterManager();
 
        manager.CourseTableAdapter = new CourseTableAdapter();
        manager.DepartmentTableAdapter = new DepartmentTableAdapter();
 
        manager.UpdateAll(ds);
    }
}

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.

Summary

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.

DotNetKicks Image
Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

4 comments

  1. Gil FinkJanuary 25, 2009 ב 7:26

    Hi Shlomo,
    With pleasure.

    Reply
  2. Rotem BloomJanuary 25, 2009 ב 10:46

    תודה אחלה של פוסט

    Reply