Skip to main content
Email Password   helpLost your password?

Introduction

When you work with relational data you often have to work with DataSets. The .NET DataSet supports AutoIncrement columns. It's quite easy to manage updates to a single table with such an identity column. Problem arises when you try to add (insert) new records to a DataSet where the tables have parent-child relationships.

Even if a database is properly designed, you can encounter the following major problems when using DataSets:

  1. The order of updating the tables must be known (you cannot update the child, while there's no parent information in the database).
  2. When there are more client applications connected to the same database with disconnected DataSets, upon update the database will generate its own identity values - which may differ from the identity values in the disconnected DataSet. So, further updates to a child table may result in error or in corrupted data.

Background

There were a few articles on this problem, but I couldn't find an elegant and easy to use solution. So, I decided to create some kind of a component - a base class, which is easy to use, and which presents a solution to this problem.

The ideas given in other articles were quiet OK, but they had some errors:

The next step

The solution is simple. I've generated a class, which has a connection string and a DataSet or DataTable as input parameter. This class will generate the SELECT, INSERT, UPDATE and DELETE statements for the table (by using SqlCommandBuilder).

AutoIncrement

The idea is to modify the INSERT statement so that we can acquire the identity generated by the database. For this we have to:

This solves the problem of AutoIncrement fields. If the parent field's AutoIncrement values are updated, this change is reflected in all the child foreign keys.

Order of update

I found an article (possibly on CodeProject) where someone was complaining about the update. The problem is that the update-order cannot be arbitrary. First the parent table must be updated, then the child. So, either you keep track of the relationships, or you create an algorithm which examines the correct order of update. This code was checked with the DataSet in picture, and also with a bigger system, where user info was stored. This problem was solved by using the DataTable.ParentRelations property.

The easiest solution:

So upon initialization, all the SQL queries and also the update order is calculated. I've finally added some functions:

There is also a function to define filters for the fill (for the query). Also, a property IsUpToDate, which returns true, if the table is not modified (no changes after the fill).

Using the code

// init and fill

ds1 = new Dataset1();
ba1 = new BaseAccessor(
     System.Configuration.ConfigurationSettings.AppSettings["ConnStr"], ds1);
ba1.AddFilter(ds1.Orders, "uid = @i_a", new object[] {55});
ba1.AddFilter(ds1.OrderedItems, "orderUid = @i_a", new object[] {55});
ba1.Fill();

// do here any inserts

// ...


// update

ba1.Update();

Points of interest

This class can be used as a base class for data manipulations.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralmySql conversion Pin
smitty3187
13:34 7 Jul '09  
Generaldatabase tables Pin
Member 1260580
23:44 29 Sep '08  
GeneralRe: database tables Pin
Member 1260580
15:15 30 Sep '08  
GeneralRe: database tables Pin
Member 1260580
16:15 30 Sep '08  
QuestionUnique constraint Pin
Mike Gentle
22:51 1 Oct '06  
GeneralIs it useful at all ? Pin
mikali
20:12 9 Oct '05  
GeneralRe: Is it useful at all ? Pin
balazs_hideghety
9:36 11 Oct '05  
Generalselect @autoIdentity = @@identity Pin
kryzchek
10:28 26 Sep '05  
GeneralRe: select @autoIdentity = @@identity Pin
balazs_hideghety
22:07 26 Sep '05  


Last Updated 25 Sep 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009