6.6. Avoiding Referential Integrity Problems When Updating the Data Source
Problem
You sometimes get referential integrity
errors when you
update a DataSet that contains related parent,
child, and grandchild records back to the underlying data source, but
want to perform the update without errors.
Solution
Use one DataAdapter for each
DataTable to update the deleted, updated, and
inserted rows as shown in the following example.
The schema of table TBL0606Parent used in this solution is shown in Table 6-2.
Table 6-2. TBL0606Parent schema
|
Column name |
Data type |
Length |
Allow nulls? |
|---|---|---|---|
|
ParentId |
|
4 |
No |
|
Field1 |
|
50 |
Yes |
|
Field2 |
|
50 |
Yes |
The schema of table TBL00606Child used in this solution is shown in Table 6-3.
Table 6-3. TBL0606Child schema
|
Column name |
Data type |
Length |
Allow nulls? |
|---|---|---|---|
|
ChildId |
|
4 |
No |
|
ParentId |
|
4 |
No |
|
Field3 |
|
50 |
Yes |
|
Field4 |
|
50 |
Yes |
The schema of table TBL0606Grandchild used in this solution is shown in Table 6-4.
Table 6-4. TBL0606Grandchild schema
|
Column name |
Data type |
Length |
Allow nulls? |
|---|---|---|---|
|
GrandchildId |
|
4 |
No |
|
ChildId |
|
4 |
No |
|
Field5 |
|
50 |
Yes |
|
Field6 |
|
50 |
Yes |
The sample uses 12 stored procedures:
SP0606_GetParentUsed to retrieve a single record from the Parent table if the optional
@ParentIdparameter is specified or all Parent records if it is notSP0606_DeleteParentUsed to delete the record specified by the
@ParentIdparameter from the Parent tableSP0606_InsertParentUsed to insert ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access