DCSIMG
Hierarchical Datasets Updates - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2013 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Hierarchical Datasets Updates

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

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# January 23, 2009 5:05 PM

Shlomo said:

תודה רבה

# January 24, 2009 8:25 PM

Gil Fink said:

Hi Shlomo,

With pleasure.

# January 25, 2009 7:26 AM

Rotem Bloom said:

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

# January 25, 2009 10:46 AM

Gil Fink said:

Thanks Rotem.

# January 25, 2009 11:22 AM